欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

async/await简单使用

程序员文章站 2022-12-21 19:53:08
function process(i) { var p = new Promise(function(resolve,reject){ setTimeout(function(){ console.log(i); resolve(i); },i*1000); }); return p; } asyn... ......
function process(i) {  
    var p = new promise(function(resolve,reject){ 
        settimeout(function(){
            console.log(i);
            resolve(i);
        },i*1000);
    });
    return p;
}
async function call(){
    let res1=await process(6);
    let res2=await process(3);
    let res3=await process(4);
    let res4=await process(9);
    let res5=await process(1);
    return res1+res2+res3+res4+res5;
} 
call()

//结果:依次间隔6,3,4,9,1秒,输出字符6,3,4,9,1。