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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.