Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access properties in async await return object

Why I can't access object properties with a . in async await return object? I know that I can access the properties like below.

let val1 = await call(3);
let val2 = await call(4);

but I'm interested if this can be done

let v = await call(3).val + await call(4).val;

const call = (x) => {

    return new Promise((resolve, reject) => {

        setTimeout(() => {
            resolve({
                val: x
            });
        }, 3000)
    })
}

const dummy = async () => {

    //let val1 = await call(3);
    //let val2 = await call(4);
    //alert(value.val + val2.val);
    let v = await call(3).val + await call(4).val;
    alert(v);
}

dummy()
like image 687
NuOne Avatar asked Jul 19 '19 12:07

NuOne


People also ask

Can we use return with await?

Using return await inside an async function keeps the current function in the call stack until the Promise that is being awaited has resolved, at the cost of an extra microtask before resolving the outer Promise.

Does await return a value?

Await expressions make promise-returning functions behave as though they're synchronous by suspending execution until the returned promise is fulfilled or rejected. The resolved value of the promise is treated as the return value of the await expression.

Can we use await inside callback?

The await keyword is used in an async function to ensure that all promises returned in the async function are synchronized, ie. they wait for each other. Await eliminates the use of callbacks in .

Does await blocks the next line?

await only blocks the execution of the next lines of code in an async function and doesn't affect the promise execution.

How to use await inside an async function?

If you want to run this with await, you will have to use it inside an async function Show activity on this post. You can simply put the async keyword on any function, not only function declarations but also function expressions and methods of object. For example:

What types can be returned by async methods?

Starting with C# 7.0, an async method can return any type that has an accessible GetAwaiter method that returns an instance of an awaiter type. In addition, the type returned from the GetAwaiter method must have the System.Runtime.CompilerServices.AsyncMethodBuilderAttribute attribute.

What does async mean before a function?

The word “async” before a function means one simple thing: a function always returns a promise. If the code has return <non-promise> in it, then JavaScript automatically wraps it into a resolved promise with that value.

How do you use async in JavaScript?

Let’s start with the async keyword. It can be placed before a function, like this: The word “async” before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.


1 Answers

You're trying to await the value of the val property of the promise.

You need to await the promise and then read the val property from the result: (await call(3)).val

const call = (x) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve({
                val: x
            });
        }, 3000)
    })
}

const dummy = async () => {
    let v = (await call(3)).val + (await call(4)).val;
    alert(v);
}

dummy()
like image 137
Quentin Avatar answered Oct 18 '22 20:10

Quentin