/
/
All You Need to Know to Get Started With JavaScript Promises 🔥
Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on email

All You Need to Know to Get Started With JavaScript Promises 🔥

Promises are a new way to handle asynchronous programming in JavaScript. They provide a more elegant and easier-to-understand alternative to callbacks. This blog post will cover everything you need to know to get started with promises, including how they work, when they should be used, and even some of the top libraries that make it easy for developers like yourself!

✍️ Here’s a simple analogy ✍️

Imagine you are a little kid, your mommy promises you that she’ll buy you a new computer next week. You don’t know if you will get that computer until next week. Mom can either buy you a new computer, or she doesn’t, because you didn’t clean your room, and she isn’t happy!🛏️

That is a promise! A promise has 3 states:

🔶Pending: You don’t know if you will get that computer.

🔶Fulfilled: Mom is happy. She buys you a new computer.

🔶Rejected: Mom is unhappy. She doesn’t buy you a computer.

const isMomHappy = true;

const willIGetNewComputer = new Promise((resolve, reject) => {
    if (isMomHappy) {
        resolve('mommy bought me a new computer!');
    } else {
        const reason = new Error('mom is not happy');
        reject(reason);
    }
});

willIGetNewComputer
    .then((momsAnswer) => {
        console.log(momsAnswer); // momy bought me a new computer!
    })
    .catch((momsAnswer) => {
        console.log(momsAnswer);
    });

🏁 Creating and Using A Promise 🏁

Firstly, we use a constructor to create a Promise object in #javascript. It takes two parameters, one for success (resolve) and one for fail (reject). Finally, there will be a condition. If the condition is met, the Promise will be resolved. Otherwise, it will be rejected:

✔️ .then( ) – for resolved:

If the Promise success, then something will happen next depends on what we do with the resolved Promise

❌ .catch( ) – for rejected:

What if the Promise fails? It will jump to the catch( ). Therefore we need to use the catch( ) method.

Conclusion 📌

Many people struggle with understanding how promises work, so I’ll try my best to explain them as simply as possible: Promises are a way of making contracts or agreements. If you promise to do something for somebody else on the condition that they will give you something in return at another time, then it is called an “agreement.” Promises work because if one person doesn’t fulfill their end of the agreement by not doing what they promised and agreed upon with someone, then there would be no reason to keep up your side either.

Recent Snippet

How to use reduce over an array of objects

To get the maximum or the minimum value from an array, we can use Math.max() and pass as arguments the numbers from which we want to pick.

How to get the highest or the lowest value in an array

To get the maximum or the minimum value from an array, we can use Math.max() and pass as arguments the numbers from which we want to pick.