Created
June 8, 2018 18:09
-
-
Save justin-lyon/3f77a55b38daa871b982e41b0d0fdc74 to your computer and use it in GitHub Desktop.
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
var fetchData = function () { | |
return new Promise(function (resolve, reject) { | |
//Fake an async action with a timeout. | |
setTimeout(function () { | |
var data = { | |
users: [ | |
{ name: 'Jack', age: 22 }, | |
{ name: 'Tom', age: 21 }, | |
{ name: 'Isaac', age: 21 }, | |
{ name: 'Iain', age: 20 } | |
]}; | |
resolve(data); | |
}, 250); | |
}); | |
}; | |
var returnOne = function() { | |
return new Promise(function(resolve, reject) { | |
setTimeout(function() { | |
resolve(1); | |
}, 1000); | |
}); | |
}; | |
fetchData() | |
.then(function(res) { | |
console.log("res", res); | |
}); | |
// Chain Logic in multiple promises. | |
returnOne() | |
.then(function(res) { | |
console.log("res", res); // -> 1 | |
return res * 2; | |
}) | |
.then(function(res) { | |
console.log("res", res); // -> 2 | |
return res * 2; | |
}) | |
.then(function(res) { | |
console.log("res", res); // -> 4 | |
return res * 2; | |
}) | |
.then(function(res) { | |
console.log("res", res); // -> 8 | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment