Skip to content

Instantly share code, notes, and snippets.

@justin-lyon
Created June 8, 2018 18:09
Show Gist options
  • Save justin-lyon/3f77a55b38daa871b982e41b0d0fdc74 to your computer and use it in GitHub Desktop.
Save justin-lyon/3f77a55b38daa871b982e41b0d0fdc74 to your computer and use it in GitHub Desktop.
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