Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js function that return a promise example

// ? created a function that returns a promise, take a parameter of a boolean
function myPromise(bool) {
  return new Promise((resolve, reject) => {
    if (bool) {
      resolve("I have succeeded");
    } else {
      reject("I have failed");
    }
  });
}

// ? call the function and pass in a boolean
myPromise(true).then((res) => console.log(res));
myPromise(false).then((res) => console.log(res)).catch((err) => console.log(err));
Source by worcraft-algeria-dz.com #
 
PREVIOUS NEXT
Tagged: #js #function #return #promise
ADD COMMENT
Topic
Name
3+2 =