是否需要将await与.then一起使用

问题描述 投票:0回答:1

我有一个异步函数

async function f1 (){
  some db operation
  return result
}

这两个代码有什么区别

await f1()
.then((result)=> something1. Can I use result here?)

f1()
.then((result)=>something2. Can I use result here?)
javascript async-await es6-promise
1个回答
0
投票

回答您的直接问题:“是否需要使用await 与

then
”:否。

您问题中的两个例子实际上没有可比性。两个示例都是相同的,但在第一个示例中,您神秘地添加了多余的

await
关键字。

您应该问的问题是“使用 Promise 有哪些不同的方法?” MDN 在文档中很好地介绍了一些内容“如何使用 Promise”

这些更新的示例具有可比性。第一个使用“传统”thenable 语法。第二个使用更新的 async/await

 语法。

1.

f1().then(result => console.log(result));

const result = await f1(); console.log(result);
    
© www.soinside.com 2019 - 2024. All rights reserved.