JS async code tutorial: from callbacks to async/await
“from callbacks to async/await”
Simple examples with different implementations based on:
You need a version of NodeJS supporting async/await (tested with 8.11.2).
Then, npm install
and npm test
This test code plays with the Star Wars API
Run npm test
and you should see the same results for all the implementations. However, you may notice that the async/await code is simpler to read than the promise based one… and far more easy to follow than the callback (hell) code ;)
Compare the getLuke()
method and you should understand why Promises were a huge improvement and async/await was waited for so long.
Performing parallel requests with async/await or promise based code is easier and you should notice that the callback solution is slower than the other two since it does not parallelize requests.
By default npm test
runs all the tests. To compare performance, run:
npm run test-callback
npm run test-promise
npm run test-async-await