美文网首页
JS async 函数深究

JS async 函数深究

作者: 风之化身呀 | 来源:发表于2020-01-09 14:56 被阅读0次

实现原理

async 函数的实现原理,就是将 Generator 函数和自动执行器,包装在一个函数里。

async function fn(args) {
  // ...
}

// 等同于

function fn(args) {
  return spawn(function* () {
    // ...
  });
}


function spawn(genF) {
  return new Promise(function(resolve, reject) {
    const gen = genF();
    function step(nextF) {
      let next;
      try {
        next = nextF();
      } catch(e) {
        return reject(e);
      }
      if(next.done) {
        return resolve(next.value);
      }
      Promise.resolve(next.value).then(function(v) {
        step(function() { return gen.next(v); });
      }, function(e) {
        step(function() { return gen.throw(e); });
      });
    }
    step(function() { return gen.next(undefined); });
  });
}

执行顺序问题

async function async1(){
    console.log('async1 start')
    await async2()
    console.log('async1 end')
}
async function async2(){
    console.log('async2')
}
console.log('script start')
setTimeout(function(){
    console.log('setTimeout') 
},0)  
async1();
new Promise(function(resolve){
    console.log('promise1')
    resolve();
}).then(function(){
    console.log('promise2')
})
console.log('script end')


//

script start
async1 start
async2
promise1
script end
async1 end
promise2
settimeout
async function testSometing() {
    console.log("执行testSometing");
    return "testSometing";
}

async function testAsync() {
    console.log("执行testAsync");
    return Promise.resolve("hello async");
}

async function test() {
    console.log("test start...");
    const v1 = await testSometing();
    console.log(v1);
    const v2 = await testAsync();
    console.log(v2);
    console.log(v1, v2);
}

test();

var promise = new Promise((resolve)=> { console.log("promise start.."); resolve("promise");});//3
promise.then((val)=> console.log(val));

console.log("test end...")
//
test start...
执行testSometing
promise start..
test end...
promise
testSometing
执行testAsync
hello async
testSometing hello async

注意:当 await 后面接 async 时,await expression,会先执行expression中的同步代码,然后等待异步结果,再用 Promise 包装异步结果

参考

相关文章

  • JS async 函数深究

    实现原理 async 函数的实现原理,就是将 Generator 函数和自动执行器,包装在一个函数里。 执行顺序问...

  • js异步编程(updating)

    js 异步编程方式: Promise,generator/yield,async/await 回掉函数 js事件监...

  • 模拟异步函数

    异步函数实现机制(async.js) 实现了支持同步函数和异步函数的声明 声明异步函数 调用异步函数

  • async和await的使用

    async函数 什么是async函数? async函数是使用async关键字声明的函数。 mdn文档:https:...

  • JS中的async/await -- 异步隧道尽头的亮光

    async函数 简单解释async函数就是Generator函数的语法糖。 Generator函数写法 async...

  • 2018-05-22

    async 函数 1. 含义 async 函数是 Generator 函数的语法糖。async函数将Generat...

  • ES8(13)、await 和 async 的用法

    async函数是使用async关键字声明的函数。 async函数是AsyncFunction构造函数的实例, 并且...

  • async函数

    async函数 async函数的含义 简单来说:async 函数就是 Generator 函数的语法糖下面是两个是...

  • ES8-async&await

    async函数是使用async关键字声明的函数,async函数是AsyncFunction构造函数的实例,返回值是...

  • async 函数(转载)

    async 函数 含义 ES2017 标准引入了 async 函数,使得异步操作变得更加方便。 async 函数是...

网友评论

      本文标题:JS async 函数深究

      本文链接:https://www.haomeiwen.com/subject/sqfgfqtx.html