JS 6
//resolve/reject arguments are the parameters in then(e) and catch(error)
const myPromise = new Promise(function (resolve, reject) {
let sampleData = [2, 4, 6, 8];
(sampleData[1]) ? resolve( "resolved string" ) : reject('An error occured!');
});
myPromise
.then(function (e) {
console.log(e); //"resolved string"
})
.catch(function (error) {
throw new Error(error); //if reject we get Error('An error occured!')
})
.finally(function () {
console.log('PROMISE COMPLETED'); //printed in any case
})Api and Fetch()
PreviousJS 4, spread and rest operators, Error constructor and object, TRY, CATCH and FINALLYNextJS 7
Last updated