Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async / await assignment to object keys: is it concurrent?

I know that doing this:

const resultA = await a()
const resultB = await b()
// code here

Is effectively

a().then( resultA => {
   b().then( resultB => {
      // code here
   })
})

Basically, a() runs then b() runs. I nested them to show that both resultA and resultB are in our scope; yet both function didn't run at once.

But what about this:

const obj = {
  result1: await a(),
  result2: await b()
}

do a() and b() run concurrently?

For reference:

const asyncFunc = async (func) => await func.call()
const results = [funcA,funcB].map( asyncFunc )

I know here funcA and funcB do run concurrently.

Bonus:

How would you represent the object assignment

const obj = {
  result1: await a(),
  result2: await b()
}

using then / callbacks?


UPDATE:

@Bergi is correct in this answer, this occurs sequentially. To share a nice solution for having this work concurrently for an object without having to piece together the object from an array, one can also use Bluebird as follows

const obj2 = Bluebird.props(obj)

http://bluebirdjs.com/docs/api/promise.props.html

like image 824
Babakness Avatar asked May 18 '17 02:05

Babakness


People also ask

Is async await concurrent?

Writing async code may be considered the first step for writing concurrent code. Since async/await is just a pattern, it doesn't make javascript any more concurrent or asynchronous than callbacks did. But it does make it exponentially easier to read it.

Does async await make it synchronous?

The async keyword does not make code synchronous. async/await is just a tool to make the syntax for interacting with promises nicer. It's still using promises. "node is asynchronous (meaning multiple lines of code execute at the same time)" that is not what asynchronous means.

Does await run in parallel?

In order to run multiple async/await calls in parallel, all we need to do is add the calls to an array, and then pass that array as an argument to Promise. all() . Promise. all() will wait for all the provided async calls to be resolved before it carries on(see Conclusion for caveat).

Can async function have more than one await?

Using one try/catch block containing multiple await operations is fine when waiting for promises created on the right hand side of the await unary operator: The await operator stores its parent async functions' execution context and returns to the event loop.


1 Answers

No, every await will stop the execution until the promise has fulfilled, even mid-expression. It doesn't matter whether they happen to be part of the same statement or not.

If you want to run them in parallel, and wait only once for their result, you have to use await Promise.all(…). In your case you'd write

const [result1, result2] = await Promise.all([a(), b()]);
const obj = {result1, result2};

How would you represent the object assignment using then / callbacks?

With temporary variables for each awaited value. Every await translates into one then call:

a().then(tmp1 => {
  return b().then(tmp2 => {
    const obj = {
      result1: tmp1,
      result2: tmp2
    };
    return …
  });
})

If we wanted to be pedantic, we'd have to pick apart the object creation:

const tmp0 = {};
a().then(tmp1 => {
  tmp0.result1 = tmp1;
  return b().then(tmp2 => {
    tmp0.result2 = tmp2;
    const obj = tmp0;
    return …
  });
})
like image 185
Bergi Avatar answered Sep 19 '22 18:09

Bergi