简体   繁体   English

Javascript异步/等待回调函数

[英]Javascript async/await callback function

I'm wondering if async/await behave the same in the following two 'super-basic' examples:我想知道 async/await 在以下两个“超级基本”示例中的行为是否相同:

async function blah1() {
  return await foo.bar().then('Done');
}

as this作为这个

async function blah2() {
  return blah3(foo.bar());
}

async function blah3(fn) {
  return await fn.then('Done');
}

or is there some important difference?还是有一些重要的区别?

  • Yes I know 'return await' is redundant but I left it in for this example :)是的,我知道 'return await' 是多余的,但我把它留在了这个例子中 :)
async function blah1() {
  return await foo.bar().then('Done');
}

blah1()

Calls foo.bar , which returns a promise, to which a then is added.调用foo.bar ,它返回一个承诺, then添加一个承诺。

The resultant promise is returned.结果承诺被返回。

async function blah2() {
  return blah3(foo.bar());
}

async function blah3(fn) {
  return await fn.then('Done');
}

blah2()

Calls foo.bar , which returns a promise, which is passed to blah3 , which adds a then .调用foo.bar ,它返回一个承诺,它被传递给blah3 ,它添加了一个then

The resultant promise is returned from blah3 to blah2 and thence to the caller.结果承诺从blah3返回到blah2 ,然后返回给调用者。

I'd say no meaningful behavioral difference.我会说没有有意义的行为差异。

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

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