最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Async function as callback - Stack Overflow

programmeradmin0浏览0评论

I just started using async/await and is confused on how it interacts with callback. For example,

fooMethod(function() {
    return Promise.resolve("foo");
});

vs

fooMethod(async function() { //add async keyword
    return "foo";
});

Must fooMethod be written in a specific way so that it can handle an async function as callback?

if fooMethod is a public library, how do I know that it is safe to add async keyword to the function?

FOLLOW UP

Express router,

app.get('/foo', function (req, res) {
  return res.send("foo");
});



app.get('/foo', async function (req, res) {
  return res.send("foo");
});

both of these function works, is it safe to use though?

I just started using async/await and is confused on how it interacts with callback. For example,

fooMethod(function() {
    return Promise.resolve("foo");
});

vs

fooMethod(async function() { //add async keyword
    return "foo";
});

Must fooMethod be written in a specific way so that it can handle an async function as callback?

if fooMethod is a public library, how do I know that it is safe to add async keyword to the function?

FOLLOW UP

Express router,

app.get('/foo', function (req, res) {
  return res.send("foo");
});



app.get('/foo', async function (req, res) {
  return res.send("foo");
});

both of these function works, is it safe to use though?

Share Improve this question edited Jul 18, 2017 at 20:33 Zanko asked Jul 18, 2017 at 19:44 ZankoZanko 4,6945 gold badges36 silver badges62 bronze badges 9
  • There is no point of using async when you don't await – Royi Namir Commented Jul 18, 2017 at 20:08
  • @RoyiNamir this is just a simple example, I was just confuse on the interaction with callback of a possibly public library – Zanko Commented Jul 18, 2017 at 20:12
  • fooMethod must be written in a specific way, it must handle promise returning function. If it doesn't, none of your examples work, if it does then both examples work. – Tamas Hegedus Commented Jul 18, 2017 at 20:15
  • @TamasHegedus That’s incorrect — if the function doesn’t do anything with the return value, then it doesn’t need to handle returned Promises differently. – Jed Fox Commented Jul 18, 2017 at 20:23
  • @JF you mean if the function just ends with cb() then we are safe to add async keyword but if the functtion ends with const result = cb() then it will not support async keyword unless it resolve the promise from the callback? – Zanko Commented Jul 18, 2017 at 20:30
 |  Show 4 more ments

2 Answers 2

Reset to default 4

Your two callbacks are equivalent. An async function is just syntactic sugar for a regular function that returns a Promise. This means that you can call an async function like a regular function. Here’s a demo:

const foo = async function (arg) {
  return arg * 2
}
const bar = function (arg) {
  return Promise.resolve().then(() => {
    return arg * 2
  })
}

const fooReturn = foo(2)
const barReturn = bar(2)
console.log('foo(2) =>', fooReturn.toString())
console.log('bar(2) =>', barReturn.toString())

fooReturn.then(fooResult => console.log('await foo(2) =>', fooResult))
barReturn.then(barResult => console.log('await bar(2) =>', barResult))

However, if the code that takes the callback wants to get a response, you won’t be able to use an async function unless the code is specially designed to check the return value of the callback function and await it if it’s a Promise.

Your two functions are equivalent, but below demonstrates how using await in an async function delays the function execution by an extra tick each time:

function syncTest() {
  console.log('sync pleted')
  return Promise.resolve('foo')
}

async function asyncTest() {
  console.log('async pleted')
  return 'foo'
}

async function awaitTest() {
  console.log('await started')
  await void 0
  console.log('await awaited')
  await void 0
  console.log('await pleted')
  return 'foo'
}

console.log('start')

syncTest().then(value => console.log(`sync resolved: ${value}`))
asyncTest().then(value => console.log(`async resolved: ${value}`))
awaitTest().then(value => console.log(`await resolved: ${value}`))

Promise.resolve()
  .then(() => console.log('tick 2 pleted'))
  .then(() => console.log('tick 3 pleted'))
  .then(() => console.log('tick 4 pleted'))

console.log('tick 1 pleted')

发布评论

评论列表(0)

  1. 暂无评论