Promises Basics, Promise.then() and Promise.catch() in JavaScript.
Promises Basics, Promise.then() and Promise.catch() in JavaScript.
Promise is an object that can create a single value for some time in the future: either a fixed value, because it is not fixed (i.e., a network error occurred).
Promises have Three states
Pending | is a initial state, means neither fulfilled nor rejected |
fulfilled | Operation was completed successfully |
rejected | Operation failed |
Outputconsole.log("this is working");function fun1(){return new Promise(function(resolve, reject){setTimeout(()=>{const error = true;if(!error){console.log('your promise has been resolved');}else{console.log('your promise has not been resolved')reject('sorry not fullfilled');}resolve();},2000);})}fun1().then(function(){console.log('thanks for resolving');}).catch(function(){console.log("very bad bro");})
this is working your promise has not been resolved very bad broasdf
Post a Comment