简体   繁体   English

使用 async/await 与使用 Promise 有何不同?

[英]How does using async/await differ from using Promises?

Recently I started using (Async & Await).最近我开始使用 (Async & Await)。 Before this, I used Promise to make my process Asynchronous.在此之前,我使用 Promise 使我的流程异步。 Like:喜欢:

example.firstAsyncRequest()
.then(firstResponse => {
    return example.secondAsyncRequest(firstResponse)
})
.then(secondResponse => {
    return example.thirdAsyncRequest(secondResponse)
})
.then(result => {
    console.log(result)
})
.catch(err => {
    console.log(err)
})

Now I am achieving this like:现在我正在实现这个:

  try {
    const firstResponse = await example.firstAsyncRequest();
    const secondResponse = await example.secondAsyncRequest(firstResponse);
    const thirdAsyncRequest = await example.thirdAsyncRequest(secondResponse);
    console.log(thirdAsyncRequest)
  }
  catch (error) {
    // Handle error
  }

In both, the code-block method executes one after another and finally throws an error if any and gets caught by catch block.在这两种情况下,代码块方法一个接一个地执行,如果有错误,最终会抛出一个错误并被catchcatch My question is, is this only a difference of syntax?我的问题是,这只是语法的不同吗? Please explain or suggest me any link to understand this better.请解释或建议我任何链接以更好地理解这一点。

Thank You谢谢你

Is there only difference of syntax?只有语法上的区别吗?

Yes.是的。 Your examples are functionally equivalent except that your second example is missing您的示例在功能上是等效的,只是缺少第二个示例

console.log(thirdAsyncRequest);

...after the third await . ...在第三次await (That variable really should be result to match the first code block, or thirdResponse to match the other two response variables). (该变量真的应该result以匹配第一码块,或thirdResponse以匹配其它两个响应变量)。

async / await is syntactic sugar around promise creation and consumption. async / await是围绕承诺创建和消费的语法糖。 It's really helpful sugar, but that's all it is.真的很有用,但仅此而已。 async functions return promises. async函数返回承诺。 await expressions let you wait for a promise to settle, either getting the fulfillment value if it's fulfilled or throwing an error if it's rejected. await表达式让你等待一个 promise 的解决,如果它被实现则获取实现值,或者如果它被拒绝则抛出一个错误。 (Since rejections are errors, you can use try / catch to handle them.) An uncaught error in an async function (whether a synchronous error or a promise rejection observed by await ) causes the promise the async function returned to be rejected with that error. (由于拒绝是错误,您可以使用try / catch来处理它们。) async函数中未捕获的错误(无论是同步错误还是await观察到的承诺拒绝)都会导致async函数返回的承诺因该错误而被拒绝. Otherwise, the function's promise is resolved to whatever the function's code returns: if it returns a non-thenable, the promise is fulfilled with that value;否则,该函数的承诺将被解析为该函数代码返回的任何内容:如果它返回一个不可然后的值,则该承诺将被该值履行; if it returns a thenable, the promise is fulfilled or rejected based on the settlement of that thenable.如果它返回一个 thenable,则根据该 thenable 的结算来实现或拒绝承诺。

(Re "resolve" vs. "fulfill" and such, I've written up this post on promise terminology .) (重新“解决”与“履行”之类的,我写了这篇关于承诺术语的文章。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM