const count = true;
let countValue = new Promise(function (resolve, reject) {
if (count) {
resolve("There is a count value.");
} else {
reject("There is no count value");
}
});
console.log(countValue);
let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)
myResolve(); // when successful
myReject(); // when error
});
// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
function(value) { /* code if successful */ },
function(error) { /* code if some error */ }
);
// make new promise ... so this object will promise us that it will hold an Asynchronous action
// some times promise resolve and some times promise get rejected
const A = new Promise((resolve, reject) => {
setTimeout(() => {
// here we pass argument if promise get resolved
resolve('Done');
// here we pass argument if promise get rejected
reject('Didn"t');
}, 3000);
});
// design handling callback function for resolve
let handleResolvedA = (massage)=>{console.log(massage)}
// design handling callback function for reject
let handleRejectedA = (massage)=>{console.log(massage)}
// do handling for both
A.then(handleResolvedA, handleRejectedA)
let hw = await new Promise((resolve)=>{
resolve(“HELLO WORLD”);
})
console.log(hw);
/*basically in resolve(whatever') the whatever is returned*/
/*hw will console.log out "HELLO WORLD" */
Method Description
all(iterable) Waits for all promises to be resolved or any one to be rejected
allSettled(iterable) Waits until all promises are either resolved or rejected
any(iterable) Returns the promise value as soon as any one of the promises is fulfilled
race(iterable) Wait until any of the promises is resolved or rejected
reject(reason) Returns a new Promise object that is rejected for the given reason
resolve(value) Returns a new Promise object that is resolved with the given value
catch() Appends the rejection handler callback
then() Appends the resolved handler callback
finally() Appends a handler to the promise