Returning an awaited value returns a Promise? (es7 async/await)
Asked Answered
S

2

17
const ret = () => new Promise(resolve => setTimeout( () => resolve('somestring'), 1000));

async function wrapper() {
    let someString = await ret();
    return someString;
}

console.log( wrapper() );

It logs Promise { <pending> }; Why does it return a Promise instead of 'somestring'?

I'm using the Babel ES7 preset to compile this.

Suds answered 1/10, 2016 at 23:38 Comment(9)
Because it is async function. That's the difference between async and normal function.Calcific
I think you can get the benefits of the async/await from inside a async function. If you console.log(someString) inside of the wrapper() function or inside of any other async function, you will get the somestring value.Acatalectic
@estus Then why does logging someString right after awaiting it output the correct value?Suds
Because you await for it. The result of async function execution is always a promise. You can await for it if you're inside another async function or unwrap the result with .then(...) if you're not.Calcific
async/await is not part of ES7.Headwind
@FelixKling async/await IS part of ES7Lindblad
@Gobliins: no, it’s part of ES2017 (ES8).Headwind
@FelixKling You are correct, i falsely assumed ES2017 == ES7Lindblad
The key here is that you can't know if the async method did await (that will resolve the promise) or not! That's why the async method always returns a promise. It may be a resolved promise (and then your await or .then() in your outer function will resolve immediately), or it is not resolved yet. You can't know from just looking at the function interface.Irrecoverable
R
20

Async functions return promises. In order to do what you want, try something like this

wrapper().then(someString => console.log(someString));

You can also await on wrapper() like other promises from the context of another async function.

console.log(await wrapper());
Reserved answered 2/10, 2016 at 0:6 Comment(2)
You sure about that 2nd example there? I get syntax errors with that.Excalibur
@JakeWilson It must be inside an async function. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Reserved
L
2

if you want your async function to return a value immediatly you can use Promise.resolve(theValue)

async waitForSomething() {
    const somevalue = await waitForSomethingElse()
    console.log(somevalue)

    return Promise.resolve(somevalue)
}

IMO the async await keywords need one more, resolve

it would be nice to write return resolve 'hello'

or just

resolve 'hello'
Launalaunce answered 16/10, 2017 at 11:49 Comment(4)
well, in the meantime i have discovered that you can just return a variable from a async method, that does the same thing as calling resolve with the variableLaunalaunce
This is the WRONG answer, "if you want your async function to return a value immediately", async function always promise no matter what.Cuticle
resolve 'hello' is not even valid JavaScript. I wonder how this answer got upvoted.Looper
@Looper right! That was why I also wrote an answer.Cuticle

© 2022 - 2024 — McMap. All rights reserved.