In this article, we will see how to use a promise to perform asynchronous tasks.
A Promise is a JavaScript object that helps us perform asynchronous tasks.
Examples of asynchronous tasks are sending AJAX requests and calling functions inside setTimeout or setInterval.
A Promise can be in one of three states: pending, resolved, or rejected.
While the value is not yet available, the Promise stays pending. Afterward, it transitions to one of the two states: resolved or rejected.
To create a new Promise object, we use the new
keyword:
const myPromise = new Promise((resolve, reject) => {
};);
The function has two parameters:
Example:
function api.getUser(username) {
return new Promise((resolve, reject) => {
// Send AJAX request
http.get(`/users/${username}`, (err, result) => {
// If there's error, we call reject()
if (err) return reject(err);
// Otherwise, use resolve() to return value
resolve(result);
});
});
};
We can decide what to do with the return value in the form of .then() and catch if there’s an error.
Promise to get data
.then(We'll print it out)
.catch(There's something wrong here)
Overall:
promise.then(
function (result) {
/* handle a successful result */
},
function (error) {
/* handle an error */
}
);
Example 1:
function getUser(username) {
return new Promise((resolve, reject) => {
http.get(`/users/${username}`, (err, result) => {
if (err) return reject(err);
resolve(result);
});
});
};
const onSuccess = (user) => console.log(user);
const onError = (err) => console.error(error);
getUser("anna").then(onSuccess, onError);
.then(onSuccess, onError) has 2 functions:
Example 2:
let promise = new Promise(function (resolve, reject) {
setTimeout(() => resolve("done!"), 1000);
});
promise.then(
(result) => alert(result), // shows "done!" after 1s
(error) => alert(error) // doesn't run
);
.catch handles the error object. It’s only used when reject() is called, which means there’s an error.
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(Error("Promise Rejected Unconditionally."));
}, 1000);
});
promise.then((res) => {
console.log(value);
});
promise.catch((err) => {
alert(err);
});
If we have a sequence of asynchronous tasks, we can use promises chaining.
We do this by chaining multiple .then() and .catch().
new Promise(function (resolve, reject) {
setTimeout(() => resolve(1), 1000);
})
.then(function (result) {
alert(result); // 1
return result * 2;
})
.then(function (result) {
alert(result); // 2
return result * 2;
})
.then(function (result) {
alert(result); // 4
return result * 2;
});
If we have several promises, we can use Promise.all() method to execute multiple promises in parallel.
The syntax:
let promise = Promise.all([...promises...]);
Promise.all takes an array of promises and returns a new promise.
Example 1
Promise.all([
new Promise((resolve) => setTimeout(() => resolve(1), 3000)), // 1
new Promise((resolve) => setTimeout(() => resolve(2), 2000)), // 2
new Promise((resolve) => setTimeout(() => resolve(3), 1000)), // 3
]).then(alert); // 1,2,3 when promises are ready
Example 2
const promise1 = Promise.resolve("Hello");
const promise2 = new Promise((resolve, reject) => setTimeout(resolve, 3000, "Bye"));
const promise3 = fetch("https://api.com/users").then(res => res.json());
Promise.all([promise1, promise2, promise3]).then(values => console.log(values));
If any of the promises is rejected, the promise returned by Promise.all immediately rejected with that error.
setTimeout() is a function that delays the execution of a callback function.
Example 1:
setTimeout(() => {
console.log("It's been 3 seconds");
}, 3000);
Example 2:
let promise = new Promise(function (resolve, reject) {
// after 1 second, show the result is "done"
setTimeout(() => resolve("done"), 1000);
});