3 ms·
Upgrading to Async/Await with JavaScript ES7
- hdhzy 9y agoAsync await while looking nice compared to bare callbacks and Promises are still not without drawbacks. For example one cannot use await inside nested callback so it's in async functions it's necessary to use for-of and avoid forEach when dealing with Promise values. * http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y... * https://spion.github.io/posts/es7-async-await-step-in-the-wrong-direction.html https://spion.github.io/posts/es7-async-await-step-in-the-wr... Current solution is just an evolutionary change merging generators and Promises.
- mikewhy 9y agoAs the article mentions in its "Loops" section. It also suggests mapping to `Promise.all` instead of `for-of-await`, but they are slightly different: Promise.all will start all the promises so they run in parallel, while `for-of-await` executes them in series.
- hdhzy 9y ago> Promise.all will start all the promises so they run in parallel, while `for-of-await` executes them in series. Not true. Promises represents computation that is already started so both Promise.all and for-of will result in the same parallel computation. Promise.all does not start anything (except the promise that waits for other promises). The difference is only when you create a promise. If you do so inside for-of loop with inner awaits then it is sequential (because next promise is created only when the previous one is resolved). The code example in the article shows inner await in for-of loop with getItem: it's the function call inside for-of-await that makes it sequential, not the loop itself. If they first made all calls to getItem, gathered results (promises) in an array and then made a for-of loop with awaits of that array elements then it'd be parallel.