0
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?

Andrzej Smyk
  • 1,684
  • 11
  • 21

3 Answers3

2

You can use the promise then callback.

getInfoByName('title').then((result) => { 
  console.log(result))
}
Guy Yogev
  • 861
  • 5
  • 14
1

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

Andrzej Smyk
  • 1,684
  • 11
  • 21
-1

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.

Niels Meima
  • 119
  • 1