1

Working with function which returns Promise object like on the screenshot. How I can get PromiseResult from this object?

Promise object

Shandy
  • 43
  • 6

3 Answers3

0

You can get the result of a promise by using .then() like so:

functionThatReturnsPromise().then((result) => {
  //do something with result
})
.catch(console.error)

Another option is to use async and await like so:

async function main() {
  const result = await functionThatReturnsPromise()
  // do something with result
}

main().then(console.log).catch(console.error)

If your environment or compiler supports top-level await you can skip the main wrapper function like so:

try {
  const result = await functionThatReturnsPromise()
  // do something with result
}
catch (err) {
  console.error(err)
}

Always remember to catch or .catch otherwise you will encounter Unhandled Promise Rejection.

Mulan
  • 129,518
  • 31
  • 228
  • 259
Travis
  • 61
  • 1
  • 6
0

You need to use .then() function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

yourFunction().then((result) => {
  console.log(result);
});
Slidein
  • 265
  • 2
  • 9
-1

if you run this code in your browser yo get the same structure:

function createPromise(){
    return new Promise((resolve)=>{
        resolve([{value:{},label:'Malmo'}])
    })
}

const rta = createPromise()
rta

to get its data you can do:

rta.then(array => console.log(array[0].label))