17 ms·
JavaScript Illustrated: Promises
- nailer 7y agoOdd for an article written in Oct 8 2019, there's no mention of ES2017's `await`. `.then()` was fine back in the day, but there's not a good reason to write new code using it in 2019.
- fimbulvetr 7y agoI don't believe that's true - I haven't had the time to test this yet, but I believe that the scenarios where a power user would prefer to have a then. Await's are definitely easier because they allow you to maintain the "procedural" feel of your code, but promises allow you to kick off several async things at once so that they take place at the same time. For instance, if I have a collection of promises that each take 10 seconds I would want them to kick off at the same time rather than sequentially - so both take 10 seconds wall clock time rather than 20 seconds one after the other. To my knowledge, the only sane way to pull this off is by using promises.all with a then. Please correct me if I'm wrong.
- davidonhacker 7y agoVery easily like this. await Promise.all([promise, promise]);
- nailer 7y agoThat's a good question. In addition to the sibling reply (which answers this perfectly) you can also do what I do. Array.prototype.forEachAsyncParallel = async function (iterateFunction) { await Promise.all(this.map(iterateFunction)); } Note 1: my app, my prototypes. Note 2: read note 1.
- jgwil2 7y agoAren't there still problems with error-handling in async/away?
- aninteger 7y agoI think I'll wait for JavaScript Illustrated: Swimsuit Edition.
- thymanl23 7y agoCheck out "The Problem with Promises in JavaScript" [0] for some gotchas with the design of js promises. [0] https://dev.to/craigmichaelmartin/the-problem-with-promises-in-javascript-5h46 https://dev.to/craigmichaelmartin/the-problem-with-promises-...
- afiori 7y agoFrom that link I quite like the solution of giving two arguments to the then. It feels very algebraic.