DEV Community

Cover image for Why JavaScript Promises are awesome

Why JavaScript Promises are awesome

Methmi on August 26, 2022

What is a Promise? A promise, in computer science, is basically a concept that handles a value that is to be produced in the future, aft...
Collapse
 
sokhavuth profile image
Sokhavuth TIN • Edited

To highlight JavaScript syntax, you just write the word JavaScript or js after the opening triple quote:

let myPromise = getPromise(URL_RETURNING_MULTIPLE_ITEMS);
promise.then(result => {
   // extracts URL of oneItem
   let oneItem = JSON.parse(result).results[0].url;
   return oneItem; // returns the URL
}).then(oneItemURL => {
   console.log(oneItemURL);
   return getPromise(oneItemURL); // new promise returned
}).then(itemData => {
   console.log(JSON.parse(itemData));
}).catch(error => {
   console.log(Error caught. In the Catch block, error);
});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
methmi profile image
Methmi

Thank you so much for that input!

Collapse
 
chiroro_jr profile image
Dennis

Great article

It really helped me think deeply about promises. A promise is basically an object that represents the result of an asynchronous operation. That result can be your desired value or an error. For example if you are making a fetch request for some posts then the desired result is the array of posts. If something goes wrong on the server then the result is an error telling you what went wrong. Either way you get a result from the promise, a desired value or an error.

Collapse
 
methmi profile image
Methmi

Thank you 🙌

Collapse
 
shshank profile image
Shshank

Nice article. Thanks for sharing. You helped me clear more points on promises which helps me a lot in my projects.

Collapse
 
methmi profile image
Methmi

Thanks, glad it helped you!

Collapse
 
silvestrik profile image
silvestrik

It was really useful. Thank’s!

Collapse
 
sebasprogrammer2021 profile image
Sebastián Londoño Valencia

same here,

Collapse
 
mightytechno profile image
Mighty

Great article

Collapse
 
methmi profile image
Methmi

Thanks!

Collapse
 
iaboelsuod profile image
Ibrahim Zahema

Great job on your first article 🎉. You totally should give observables (and rxjs) a look though.

Collapse
 
methmi profile image
Methmi

Will do 🙌 Thanks for the suggestion!