admin管理员组文章数量:1336632
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'tawait
– 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 withconst 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
2 Answers
Reset to default 4Your 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')
本文标签: javascriptAsync function as callbackStack Overflow
版权声明:本文标题:javascript - Async function as callback - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742411647a2469909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论