3 ms·
The comments about a function call waiting until an argument is defined is a potentnially misleading way of thinking about await. The calls to saveToCloudFirest
by thethirdone 7y ago
The comments about a function call waiting until an argument is defined is a potentnially misleading way of thinking about await. The calls to saveToCloudFirestore and sendEmailInSendgrid do not execute in parallel. await only really replaces .then()s and can't (as far as I know) achieve an effect like Promise.all()
I don't understand the line:
> return; // the return is only here because .then() callbacks need a return
I have never seen anyone do this before and have seen many .then() callbacks work. As far as I can tell `return;` is equivalent to letting the function reach the end.
Nitpick: in the sendEmailInSendgrid function, msg is never defined. I assume res.msg or res was meant.
- johnsonjo 7y ago> await only really replaces .then()s and can't (as far as I know) achieve an effect like Promise.all() Although it's not completely equivalent to a Promise.all, you can actually use vanilla await with basically the same effect as promise.all(). Like so: [0] const timeout = ms => new Promise(res => setTimeout(res, ms)); (async () => { const startTime = Date.now(); const first = timeout(1000); const second = timeout(500); const third = timeout(800); await first; await second; await third; console.log(`Function completed in ${Date.now() - startTime} ms`) })(); This should output the completion time as somewhere very near to 1000ms. The reason they aren't completely equivalent is if one promise fails in Promise.all they all immediately fail whereas this only throws once you await the failed promise. Of course you could also use `await Promise.all([...])` in async/await too. You could try this in your browser console (I got it to log the output in chrome, but not safari for some reason) or you could go to link [0] and try it on jsfiddle. [0]: https://jsfiddle.net/johnsonjo4531/r5fasqdo/1/ https://jsfiddle.net/johnsonjo4531/r5fasqdo/1/
- vimslayer 7y agoNote that this won't work concurrently if the promises are some kind of "lazy" promise implementation that executes the operation only when `.then` is called. Like with Knex, for example. I prefer const [a, b] = await Promise.all([promiseA, promiseB])