What is a Promise in JavaScript?

A Promise represents a result that is not ready yet. It has three states: pending, fulfilled (success) and rejected (error).

const p = new Promise((resolve, reject) => {
  setTimeout(() => resolve("Data loaded"), 500);
});

p.then(value => console.log(value))
 .catch(err => console.log(err))
 .finally(() => console.log("Finished"));

Output:

Tutorials Class - Output Window

Data loaded
Finished

See Callbacks and Promises.


Learn more about the similar topics:
Tutorials
JavaScript Callbacks and Promises
JavaScript Async Await and Fetch API
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is a callback function in JavaScript?
What is async and await in JavaScript?
What is a Promise in JavaScript?
What is the event loop in JavaScript?