javascript - emscript, async 与 await (看另一个,搜索 promise, async)

访问量: 1012

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function

await 只能用在async 函数中, 能让该async函数暂停。并出让它的控制权。

例如:

function resolve_after_2s(){
    return new Promise(
      resolve => {
         setTimeout(() => {
           resolve('resolved');
         }, 2000);
      }
    ) 
    
}

async function myCall(){
    console.info("step1")
    let result = await resolve_after_2s()
    console.info("step2, " ,  result)
}

订阅/RSS Feed

Subscribe