Skip to content

Latest commit

 

History

History
24 lines (19 loc) · 1.12 KB

69-Creating-Promise.md

File metadata and controls

24 lines (19 loc) · 1.12 KB

Building a Simple Promise

To create a simple Promise in JavaScript, you can use the following syntax:

const myPromise = new Promise((resolve, reject) => {
  // Do some asynchronous operation here
  // If successful, call resolve()
  // If there's an error, call reject()
});

You can replace the comments with actual code that performs an asynchronous operation, such as making an HTTP request or reading a file. Within the Promise constructor function, you can call the resolve() method to indicate that the operation was successful and pass any data you want to return. Alternatively, you can call the reject() method to indicate that an error occurred and pass an error message or object.

Once you've created a Promise, you can use methods like then() and catch() to handle the results of the operation. For example:

myPromise.then(data => {
  // Handle successful result here
}).catch(error => {
  // Handle error here
});

The then() method takes a function that will be called if the Promise is resolved successfully, and the catch() method takes a function that will be called if the Promise is rejected.