Chaining multiple promises:
We can work with multiple promises. We can do this by using the .then() function.
var firstMethod = function () {var promise = new Promise(function (resolve, reject) {setTimeout(function () {console.log("first method completed");resolve({ data: "123" });}, 2000);});return promise;};var secondMethod = function (someStuff) {var promise = new Promise(function (resolve, reject) {setTimeout(function () {console.log("second method completed");resolve({ newData: someStuff.data + " some more data" });}, 2000);});return promise;};var thirdMethod = function (someStuff) {var promise = new Promise(function (resolve, reject) {setTimeout(function () {console.log("third method completed");resolve({ result: someStuff.newData });}, 3000);});return promise;};firstMethod().then(secondMethod).then(thirdMethod);
Post a Comment