async function getInfoByName(name) {
return await search.getInfo(name);
}
console.log(getInfoByName('title'));
It returns Promise { <Pending> }
, how can I return the value that I need?
async function getInfoByName(name) {
return await search.getInfo(name);
}
console.log(getInfoByName('title'));
It returns Promise { <Pending> }
, how can I return the value that I need?
You can use the promise then callback.
getInfoByName('title').then((result) => {
console.log(result))
}
By using Promise.prototype.then():
getInfoByName('title').then(function(value) {
console.log(value);
});
It basically impossible to return value from asynchronus call inside synchronus function. You can pass callback to your asynchronus and call it in then
section. Please see How do I return the response from an asynchronous call? for more explanation and examples
In your function 'getInfo' you should 'resolve' the value you want to return. You can do this in the promise which is in the 'getInfo' function.