const anotherPromise =newPromise((resolve, reject)=>{setTimeout(()=>{resolve('this is the eventual value the promise will return');},300);});// CONTINUATION
anotherPromise
.then(value=>{console.log(value)})
JS
copy
let done =trueconst isItDoneYet =newPromise((resolve, reject)=>{if(done){const workDone ='Here is the thing I built'resolve(workDone)}else{const why ='Still working on something else'reject(why)}})
const studentRol=newPromise((resolve, reject)=>{setTimeout(()=>{/**this function will give the value Of tageted studebt Roll number */letRollOffStd=[1,2,3,4,5,6,7,8];for(let index =RollOffStd[RollOffStd.length-1]+1; index <50; index++){RollOffStd.push(index)}resolve(RollOffStd)reject('My name is Noor mohammad ')},1000)})const mybiodata=(gRollOfStudent /* this is First parameter OF ( mybiodata function ) and You can change parameter value */)=>{returnnewPromise((resolve, reject)=>{setTimeout((x)=>{let bio={myname:'Noor mohammad Patwary ',age:25,}resolve(`my name is ${bio.myname} and my age = ${bio.age} and my roll is =${x}`)},1000,gRollOfStudent);})}
studentRol.then((RollOfStudent)=>{console.log(RollOfStudent);/** From here we are gating the Value OF student roll number */mybiodata(RollOfStudent[1]/* this is First Argument OF ( mybiodata function )*/).then((fff)=>{console.log(fff);})}).catch((x)=>{console.log(x);})
/*
A promise is a building object of JavaScript, using it we can easily do
asynchronous tasks. Also, the concept that is used to create clean code
basically promises.
*///Promiselet firstPromise =newPromise((resolved, reject)=>{let fullName ='Muhammad Shahnewaz';setTimeout(()=>resolved(fullName),3000);//we need to use setTimeout() }).then((name)=>{console.log('I am '+ name);//Muhammad Shahnewaz});
const count =true;let countValue =newPromise(function(resolve, reject){if(count){resolve("There is a count value.");}else{reject("There is no count value");}});console.log(countValue);
let myPromise =newPromise(function(myResolve, myReject){// "Producing Code" (May take some time)myResolve();// when successfulmyReject();// 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 rejectedconstA=newPromise((resolve, reject)=>{setTimeout(()=>{// here we pass argument if promise get resolvedresolve('Done');// here we pass argument if promise get rejectedreject('Didn"t');},3000);});// design handling callback function for resolve lethandleResolvedA=(massage)=>{console.log(massage)}// design handling callback function for reject lethandleRejectedA=(massage)=>{console.log(massage)}// do handling for bothA.then(handleResolvedA, handleRejectedA)
// ? created a function that returns a promise, take a parameter of a booleanfunctionmyPromise(bool){returnnewPromise((resolve, reject)=>{if(bool){resolve("I have succeeded");}else{reject("I have failed");}});}// ? call the function and pass in a booleanmyPromise(true).then((res)=>console.log(res));myPromise(false).then((res)=>console.log(res)).catch((err)=>console.log(err));
functionexamplePromise(){let promiseToReturn =newPromise(function(resolve, reject){let sum;setTimeout(function(){
sum =5+6;if(sum >10){resolve(sum);}else{reject('The promise has been rejected');}},2000);});return promiseToReturn;}console.log('some piece of code');examplePromise().then(function(result){console.log(result);}).catch(function(error){console.log(error);});console.log('another piece of code');
MethodDescriptionall(iterable)Waitsfor 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 newPromise object that is rejected for the given reason
resolve(value)Returns a newPromise 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