async function getInfoByName(name) {
return await search.getInfo(name);
}
console.log(getInfoByName('title'));
它返回Promise { <Pending> }
,我该如何返回我需要的值?异步并等待nodejs
async function getInfoByName(name) {
return await search.getInfo(name);
}
console.log(getInfoByName('title'));
它返回Promise { <Pending> }
,我该如何返回我需要的值?异步并等待nodejs
getInfoByName('title').then(function(value) {
console.log(value);
});
它基本上不可能从SYNCHRONUS函数内部asynchronus调用的返回值。您可以将回拨传递给您的异步,并在then
部分中调用它。请参阅How do I return the response from an asynchronous call?以获取更多解释和示例
在函数'getInfo'中,您应该'解析'您想要返回的值。你可以在'getInfo'函数中的承诺中做到这一点。
我做了 返回新的Promise(resolve => {resolve(“True”)} 举例 –
您可以使用承诺then callback。
getInfoByName('title').then((result) => {
console.log(result))
}
我做了一个控制台日志,它的工作原理,但如何从函数返回此值? –