Promise basically used for async, because other function will execute if before function is fine.
–JS
var fPromise = new Promise(function (resolve, reject){ var isTrue = true; if(isTrue){ resolve('True'); } else { reject(); } }); fPromise.then(function(fromRes){ console.log('this is ' + fromRes); });
If Promise not completed.
–JS
var fPromise = new Promise(function (resolve, reject){ var isTrue = false; if(isTrue){ resolve('True'); } else { reject('Not True'); } }); fPromise.then(function(fromRes){ console.log('this is ' + fromRes); }).catch(function(fromRej){ console.log('this is ' + fromRej); })
Now Promises one by one.
–JS
var fPromise = function(){ return new Promise(function(resolve, reject){ resolve('done fPromise completed.'); }) }; var sPromise = function(msg){ return new Promise(function(resolve, reject){ resolve(msg + ' done sPromise'); }) }; var tPromise = function(msg){ return new Promise(function(resolve, reject){ resolve(msg + ' done tPromise'); }) }; fPromise().then(function(res){ return sPromise(res); }).then(function(res){ return tPromise(res); }).then(function(res){ console.log('done by ' + res); })