function with parantersthat returns a promise code example

Example 1: promise with params

function someFunction(username, password) {
  return new Promise((resolve, reject) => {
    // Do something with the params username and password...
    if ( /* everything turned out fine */ ) {
      resolve("Stuff worked!");
    } else {
      reject(Error("It didn't work!"));
  	}
  });
}

someFunction(username, password)
  .then((result) => {
    // Do something...
  })
  .catch((err) => {
  	// Handle the error...
  });

Example 2: making promises in js

getData()
    .then(data => console.log(data))
    .catch(error => console.log(error));

Tags:

Misc Example