The below Javascript example will create a Promise object which will pass the revised data of an array in another array object to the function in the then method of that promise if the promise has been resolved or else will pass the error message to the function in the catch method of that promise object if that promise object has been rejected.
The element in the original array will get multiplied by itself and then another array will get created with the revised data set.
let p = new Promise((resolve, reject) => { try { let data = [2, 4, 5, 6]; let data_revise = data.map(x => x * x); resolve(data_revise) } catch(err) { reject("error happened " + err) } })
The above line of code will create a promise object which will then carry out the paragraph two operation.
p.then((data) => { console.log(data) }).catch(function(error){ console.log(error) });
The above line of code will carry out the paragraph one operation!
The outcome is as follows:-
(4) [4, 16, 25, 36]
If the error happened during the multiplication process then the error message will get printed instead!