小编vrs*_*ion的帖子

就返回值而言,异步函数中的await 关键字有什么意义?

我研究了很多帖子、文章和文档。我正在寻求更深入的理解。

来自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

await表达式导致async函数执行暂停,直到 aPromise确定,[...]

let fetchLocation = () => {
  //fetches information from satellite system and 
  //returns a Vector2 of lat long on earth
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        lat: 73,
        long: -24
      });
    }, 2000);
  });
}

//async functions automatically return a promise.
let main = async() => {

  //awaits the promises resolution
  let result = await fetchLocation();
  console.log("I'm the awaited promise result", result)

  //immediately returns a promise …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous async-await

3
推荐指数
1
解决办法
1441
查看次数

标签 统计

async-await ×1

asynchronous ×1

javascript ×1