简体   繁体   中英

Javascript async/await callback function

I'm wondering if async/await behave the same in the following two 'super-basic' examples:

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 :)
async function blah1() {
  return await foo.bar().then('Done');
}

blah1()

Calls foo.bar , which returns a promise, to which a then is added.

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 .

The resultant promise is returned from blah3 to blah2 and thence to the caller.

I'd say no meaningful behavioral difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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