i'm struggling bit async/await , returning value promise.
function test () { return new promise((resolve, reject) => { resolve('hello') }) } async function c() { await test() }
as understood things should able value doing:
console.log(c())
but missing point here returns promise. shouldn't print "hello"? on similar note unclear whether callback needs converted promise before wrapping in async/await?
i missing point here returns promise. shouldn't
console.log(c())
print "hello"?
no, async
functions return promises. they're not magically running asynchronous code synchronously - rather reverse, turn synchronous-looking code (albeit speckled await
keywords) asynchronously running one.
you can result value inside asynchronous function:
async function c() { const result = await test() console.log(result); return 'world'; } c().then(console.log);
i unclear whether callback needs converted promise before wrapping in async/await?
yes, can await
promises. see how convert existing callback api promises? how conversion.
Comments
Post a Comment