How to store a value returned by a promise in a variable? | Sololearn: Learn to code for FREE!

+3

How to store a value returned by a promise in a variable?

https://code.sololearn.com/WZaCIW7SWVpr/?ref=app

4/2/2022 8:53:29 AM

Kashyap Kumar

4 Answers

New Answer

+3

Promises are another way to handle asynchronous programming in JavaScript. When created, a promise starts in the pending state and enters the fulfilled or the rejected state depending on the result of the asynchronous work. To handle the result of a promise, we use the .then(), .catch(), and .finally() member functions. The .then() function takes in two handler functions, one for promise fulfillment and one for promise rejection. The .catch() function takes in only one function and handles promise rejection. Promise.finally() takes in one function and is called for either a promise fulfillment or rejection.

+3

/* The value is always there in the promise. We just have move to next step using 'then()' to get the data. For example in your code this can be added in the end. */ title.then((data) => { console.log(data.title) })

+1

Maybe this can help you: https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise