async和yield同步的研究

一、async,await用法
await后面接promise对象,并且resolve。
或者接基本类型,如 await 400,会转换为promise对象,并且 resolve(400)

function getSomeThing(){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve('获取成功')
        },3000)
    })
}
async function getDate(){
  const res1 = await getSomeThing("./a.json")
  console.log(res1)
  
  const res2 = await getSomeThing("./b.json")
  console.log(res2)
  
  const res3 = await getSomeThing("./c.json")
  console.log(res3)
}
function call(){
    console.log('call funtion');
    return new Promise(resolve=>{
        setTimeout(function(){
            console.log('call funtion timeout');
            resolve('dd');
        },1000);        
    });

}
function normalFunction() {
    console.log('normalFunction');
    return 'data'
}
// call(function(){
//     console.log('callback');
// });
async function asynctest() {
    console.log('start');
    await call();
    await normalFunction();
    await new Promise(resolve=>{ console.log('wait result'); resolve()});
    console.log('end');
}
asynctest();

执行结果:

start
call funtion
call funtion timeout
normalFunction
wait result
end

二、* 函数, yield和next()用法
参考例子:https://zhuanlan.zhihu.com/p/31742488
yield是JS为了解决异步调用的命令。表示程序执行到这里会交出执行权,等待结果返回。它需要在协程Generator 函数中运行。
上面代码中,调用 Generator 函数,会返回一个内部指针(即遍历器g 。这是 Generator 函数不同于普通函数的另一个地方,即执行它不会返回结果,返回的是指针对象。调用指针 g 的 next 方法,会移动内部指针(即执行异步任务的第一段),指向第一个遇到的 yield 语句,上例是执行到 x + 2 为止。

function call(){
    console.log('call funtion');
    return new Promise(resolve=>{
        setTimeout(function(){
            console.log('call funtion timeout');
            resolve('dd');
        },1000);        
    });

}
function normalFunction() {
    console.log('normalFunction');
    return 'data'
}
function* yieldFunc() {
    console.log('start');
    yield call();
    yield normalFunction();
    console.log('end');
}
let yieldData=yieldFunc();
let firstData=yieldData.next();
console.log(firstData);
firstData.value.then(function(data){
    console.log(data);
});
yieldData.next();
console.log(yieldData);

执行结果:

PS F:\zzj1026\Rnx\testEgg> node .\async.js
start
call funtion
{ value: Promise { <pending> }, done: false }
normalFunction
{}
call funtion timeout
dd
PS F:\zzj1026\Rnx\testEgg>

三、Promise.all()

const promise1 = Promise.resolve(3)
      const promise2 = 42
      const promise3 = new Promise((resolve, reject) => {
        setTimeout(reject, 100, 'foo')
      })

      Promise.all([promise1, promise2, promise3])
        .then((values) => {
          console.log(values)
        })
        .catch((values) => console.log('reject', values))
      // expected output: Array [3, 42, "foo"]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端段

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值