Introduction
A promise is an object that may produce a single value some time in the future: either a resolved value or a reason that it's not resolved (e.g., a network error occurred). Promises are used in modern JavaScript for a variety of purposes, including:
As a return value from async functions.
As arguments for Promise-accepting functions.
To schedule async tasks.
To load data asynchronously.
To handle errors in async code.
A promise may be in one of three possible states: fulfilled, rejected, or pending.
When a promise is fulfilled, the associated callbacks are called with the promise's resolved value. When a promise is rejected, the associated callbacks are called with the promise's rejection reason. If a promise is pending, the associated callbacks are not called until the promise is either fulfilled or rejected. Users can attach callbacks to handle the fulfilled value or the reason for rejection. Promises are used in many modern JavaScript libraries and frameworks, including AngularJS, jQuery, Node.js, and others.
A promise can be created in a number of ways, including:
-By using the Promise constructor.
-By using a promise-returning async function.
-By using a promise-accepting function.
Once created, its value can be accessed by using one of the promise's then methods. A promise can also be rejected by using the promise's catch method.
# Creating Promises
The simplest way to create a promise is to use the Promise constructor. The Promise constructor takes a function as an argument. The function is passed on two arguments: resolve and reject. resolve is a function that is used to resolve the promise with a value. reject is a function that is used to reject a promise with a reason.
The following example creates a promise that resolves with the string "Hello, world!" after one second:
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
Promises can also be created by using promise-returning async functions. An async function is a function that returns a promise. The following example creates a promise that resolves with the string "Hello, world!" after one second:
async function example() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
}
Promises can also be created by using promise-accepting functions. A promise-accepting function is a function that takes a promise as an argument. The following example creates a promise that resolves with the string "Hello, world!" after one second:
function example(promise) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
}
A promise can also be created using the Promise constructor:
const promise = new Promise((resolve, reject) => {
// do something asynchronous which eventually calls either:
// resolve(someValue); // fulfilled
// or
// reject("failure reason"); // rejected
});
And chained using the then method:
promise.then(
(value) => {
// success
},
(reason) => {
// failure
}
);
The first argument then is a function that will be called if the promise is resolved, and the second argument is a function that will be called if the promise is rejected.
Accessing Promise Values
Once a promise is created, its value can be accessed by using one of the promise's then methods. The then method takes two arguments: a success callback and an error callback. The success callback is called if the promise is resolved successfully. The error callback is called if the promise is rejected.
The following example logs the string "Hello, world!" to the console after one second:
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hello, world!');
}, 1000);
});
promise.then((value) => {
console.log(value);
});
# Rejecting Promises
A promise can be rejected by using the promise's catch method. The catch method takes a function as an argument. The function is called if the promise is rejected.
The following example logs an error to the console if the promise is rejected:
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error('Promise rejected'));
}, 1000);
});
promise.catch((error) => {
console.error(error);
});
# Conculusion
Promises are a powerful tool for managing asynchronous code and can make code much more readable and maintainable.