我正在尝试在我的代码中实现async / await。但我对此表示怀疑。
代码:
const a = async() => {
var Param = { key: 'xxx' };
const authusers = await func1(Param);
await console.log("hello world");
}
func1() {
dbquery {
console.log(results);
return results;
}
}
根据我对async / await的理解,只有在第一次await函数之后才会执行第二个await函数,结果将是:
结果
你好世界
但结果显示为:
你好世界 结果答案 0 :(得分:4)
async / await与Promises一起使用,因此您无法返回结果,您需要返回Promise。
您不需要等待同步代码"例如控制台日志",仅限于您的承诺功能。
const a = async() => {
var Param = { key: 'xxx' };
const authusers = await func1(Param);
console.log(authusers)
console.log("hello world");
}
const func1 = (param) =>{
return new Promise(resolve =>{
setTimeout(() =>{
return resolve('waited')
},1000)
})
}
a()