const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data fetched");
}, 2000);
});
console.log("Start");
promise.then((message) => {
console.log(message);
});
console.log("End");
In this example, the promise is created with a function that takes two arguments: resolve
and reject
. The resolve
function is called when the operation is successful, while reject
is called if an error occurs. The then
method is used to handle the resolved value.