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

bool3max picture bool3max · Oct 2, 2016 · Viewed 14.8k times · Source
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.

Answer

afuous picture afuous · Oct 2, 2016

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());