← Back to context

Comment by tsian2

4 years ago

I'm new to NodeJS but it was my understanding that using await like that would help by not blocking the thread, leaving node free to process other requests.

await won't block Node from handling other requests but it will block the thread that is awaiting. For example, these will execute one after the other (the "bad way"):

  await slowThingOne();
  await slowThingTwo();

but these execute in parallel, awaiting until both are finished (the "good way"):

  await Promise.all([slowThingOne(), slowThingTwo()]);

  • Minor clarification, the latter example will only await until both have resolved *or* one rejects. To await the resolution of all promises, you should use the truly gamechanging `Promise.allSettled` , though you will have to transpile it if it's not supported by your target environments.