项目作者: pierreroth64

项目描述 :
JS async code tutorial: from callbacks to async/await
高级语言: JavaScript
项目地址: git://github.com/pierreroth64/js-async-tutorial.git
创建时间: 2017-03-20T13:49:20Z
项目社区:https://github.com/pierreroth64/js-async-tutorial

开源协议:MIT License

下载


JS async tutorial Travis-CI Status

“from callbacks to async/await”

Simple examples with different implementations based on:

  • callbacks
  • promises
  • async/await

Prerequisite / install

You need a version of NodeJS supporting async/await (tested with 8.11.2).

Then, npm install and npm test

Details

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.

Performance

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

Test run