返回期待值会返回一个Promise吗? (es7 async / await)
问题描述:
const ret = () => new Promise(resolve => setTimeout( () => resolve('somestring'), 1000));
async function wrapper() {
let someString = await ret();
return someString;
}
console.log( wrapper() );
它记录承诺{< pending> }
;
为什么它返回Promise而不是'somestring'
?
我正在使用Babel ES7预设为编译。
I'm using the Babel ES7 preset to compile this.
答
异步函数返回promises。为了做你想做的事,试试这样的事情
Async functions return promises. In order to do what you want, try something like this
wrapper().then(someString => console.log(someString));
您还可以等待 wrapper()
与其他异步函数的上下文中的其他promise一样。
You can also await on wrapper()
like other promises from the context of another async function.
console.log(await wrapper());