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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
bool3max
  • 2,748
  • 5
  • 28
  • 57
  • Because it is async function. That's the difference between async and normal function. – Estus Flask Oct 01 '16 at 23:44
  • 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. – Diego Cardoso Oct 01 '16 at 23:46
  • @estus Then why does logging `someString` right after awaiting it output the correct value? – bool3max Oct 01 '16 at 23:46
  • 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. – Estus Flask Oct 02 '16 at 00:34
  • `async/await` is not part of ES7. – Felix Kling Oct 03 '16 at 14:50
  • @FelixKling async/await IS part of ES7 – Gobliins Jul 23 '18 at 09:44
  • 1
    @Gobliins: no, it’s part of ES2017 (ES8). – Felix Kling Jul 23 '18 at 11:27
  • 1
    @FelixKling You are correct, i falsely assumed ES2017 == ES7 – Gobliins Jul 23 '18 at 11:52
  • 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. – Andreas L Nov 14 '19 at 12:50

2 Answers2

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());
afuous
  • 1,478
  • 10
  • 12
  • You sure about that 2nd example there? I get syntax errors with that. – Jake Wilson May 16 '17 at 21:26
  • @JakeWilson It must be inside an async function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await#Examples – afuous May 16 '17 at 22:05
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'
  • 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 variable – Martijn Scheffer Aug 19 '18 at 16:29
  • 1
    This is the WRONG answer, "if you want your async function to return a value immediately", async function always promise no matter what. – Qiulang Aug 30 '23 at 03:50
  • `resolve 'hello'` is not even valid JavaScript. I wonder how this answer got upvoted. – Bergi Aug 30 '23 at 04:30
  • @Bergi right! That was why I also wrote an answer. – Qiulang Aug 30 '23 at 06:25