How to store a value returned by a promise in a variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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

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

2nd Apr 2022, 8:53 AM
Kashyap Kumar
Kashyap Kumar - avatar
3 Answers
+ 4
/* 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) })
8th Apr 2022, 12:42 AM
Morpheus
Morpheus - avatar
+ 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.
7th Apr 2022, 3:56 PM
Calviղ
Calviղ - avatar
2nd Apr 2022, 11:41 AM
PanicS
PanicS - avatar