Link Search Menu Expand Document

Complete a Promise with resolve and reject

Summary

  • A promise has three states: pending, fulfilled, and rejected.
  • Use conditions to determine the outcome of a promise.

Final Code

const makeServerRequest = new Promise((resolve, reject) => {
  // responseFromServer represents a response from a server
  let responseFromServer;
    
  if(responseFromServer) {
    resolve("We got the data")
  } else {  
    reject("Data not received")
  }
});