Skip to content

Instantly share code, notes, and snippets.

@devarajchidambaram
Last active August 28, 2018 10:52
Show Gist options
  • Save devarajchidambaram/4d1fe322dd60d1eef9871acb602ceb08 to your computer and use it in GitHub Desktop.
Save devarajchidambaram/4d1fe322dd60d1eef9871acb602ceb08 to your computer and use it in GitHub Desktop.
multiple then in the promises usage
/*
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