Last active
August 28, 2018 10:52
-
-
Save devarajchidambaram/4d1fe322dd60d1eef9871acb602ceb08 to your computer and use it in GitHub Desktop.
multiple then in the promises usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
USE OF MUTIPLE THEN IN THE PROMISES | |
Actually in then we can return a sync value or | |
multiple async values | |
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining | |
**/ | |
var p1 = new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log("waited for 2000 ms") | |
resolve('waited 2000 ms') | |
}, 2000) | |
}).then(function (data) { | |
//HERE WE CAN RETURN THE PROMISE AS VALUE | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log("waited for 3000 ms") | |
resolve('waited 3000 ms') | |
}, 3000) | |
}) | |
}).then(function (data) { | |
//HERE ALSO DOING THE SAME | |
return new Promise(function (resolve, reject) { | |
setTimeout(function () { | |
console.log("waited for 4000 ms", data) | |
resolve('waited 4000 ms') | |
}, 4000) | |
}) | |
}).then(function (data) { | |
console.log("Finally????", data) | |
}).catch(function (err) { | |
console.log("errr", err) | |
}).finally(function () { | |
console.log("clear the values finally") | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment