js make promise code example
Example 1: js create a promise
/*
A Promise is a proxy for a value not necessarily known when the promise is created.
It allows you to associate handlers with an asynchronous action's eventual success
value or failure reason.
*/
let promise = new Promise((resolve , reject) => {
fetch("https://myAPI")
.then((res) => {
// successfully got data
resolve(res);
})
.catch((err) => {
// an error occured
reject(err);
});
});
Example 2: promise javascript
/*
Promise is a constructor function, so you need to use the new keyword to
create one. It takes a function, as its argument, with two parameters -
resolve and reject. These are methods used to determine the outcome of the
promise.
The syntax looks like this:
*/
const myPromise = new Promise((resolve, reject) => {
});