Skip to content

Latest commit

 

History

History
29 lines (21 loc) · 1.7 KB

61-Creating-Promise.md

File metadata and controls

29 lines (21 loc) · 1.7 KB

what is Building a Simple Promise

Promises in JavaScript are used to handle asynchronous operations like fetching data from a database or making an API call. A Promise represents the eventual completion (or failure) of an asynchronous operation and allows you to handle that result when it becomes available.

To build a simple Promise in JavaScript, you can use the Promise constructor function which takes a callback function with two arguments: resolve and reject. The resolve function is called when the Promise is fulfilled with a successful result, while the reject function is called when there's an error.

Here's an example of building a simple Promise which returns a message after a delay of 2 seconds:

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("Hello World!");
  }, 2000);
});

myPromise
  .then((result) => {
    console.log(result); // prints 'Hello World!' after 2 seconds
  })
  .catch((error) => {
    console.error(error);
  });

In this example, we create a new Promise using the Promise constructor and pass in a callback function which uses the setTimeout function to delay the execution of the resolve function by 2 seconds. When the timeout is complete, the resolve function is called with the message 'Hello World!'.

We then chain the .then() method to the Promise object which will execute its callback function when the Promise is fulfilled successfully. The console.log statement inside the callback function prints the result of the Promise, which is the message 'Hello World!'.

If there was an error during the execution of the Promise, the .catch() method would be executed, which logs the error to the console with console.error.