
Upgrading to Async/Await with JavaScript ES7 - bbrennan
https://medium.com/@bbrennan/upgrading-to-async-await-with-javascript-es7-3c67602ea7f8
======
hdhzy
Async 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-y...](http://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/)

* [https://spion.github.io/posts/es7-async-await-step-in-the-wr...](https://spion.github.io/posts/es7-async-await-step-in-the-wrong-direction.html)

Current solution is just an evolutionary change merging generators and
Promises.

~~~
mikewhy
As 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
> 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.

