https://es6.ruanyifeng.com/#docs/async#async-%E5%87%BD%E6%95%B0%E7%9A%84%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86

    1. async function fn(args) {
    2. // ...
    3. }
    4. // 等同于
    5. function fn(args) {
    6. return spawn(function* () {
    7. // ...
    8. });
    9. }
    10. // 具体实现
    11. function spawn(genF) {
    12. return new Promise(function(resolve, reject) {
    13. const gen = genF();
    14. function step(nextF) {
    15. let next;
    16. try {
    17. next = nextF();
    18. } catch(e) {
    19. return reject(e);
    20. }
    21. if(next.done) {
    22. return resolve(next.value);
    23. }
    24. Promise.resolve(next.value).then(function(v) {
    25. step(function() { return gen.next(v); });
    26. }, function(e) {
    27. step(function() { return gen.throw(e); });
    28. });
    29. }
    30. step(function() { return gen.next(undefined); });
    31. });
    32. }