Working with function which returns Promise object like on the screenshot. How I can get PromiseResult from this object?
Asked
Active
Viewed 1,686 times
1
-
result.then((data) => ... // data is your result array) – andy mccullough Apr 07 '22 at 17:00
-
someFunction(...yourArguments).then(result=>doSomethingWith(result)) – The Bomb Squad Apr 07 '22 at 17:00
-
`const result = await mypromise` – Mulan Apr 07 '22 at 17:31
3 Answers
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.
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))

braian azcune
- 36
- 3