Skip to content

Instantly share code, notes, and snippets.

@w3cj
Last active December 3, 2015 23:35
Show Gist options
  • Select an option

  • Save w3cj/84c193aca1fe4a6916c8 to your computer and use it in GitHub Desktop.

Select an option

Save w3cj/84c193aca1fe4a6916c8 to your computer and use it in GitHub Desktop.
getUserData().then(function (userData) {
return getUserMessages(userData.id).then(function(userMessages){
return { userData: userData, userMessages: userMessages }
});
}).then(function (result) {
return getUserLocation(result.userData.id).then(function(userLocation){
result.userLocation = userLocation;
return result;
});
}).then(function(result){
renderData(result.userData, result.userMessages, result.userLocation)
});
function getUserData () {
return new Promise(function(resolve, reject){
var data = {name: "Danny", Age: 26, id: 1211}
//setTimeout roughly simulates an HTTP request.
setTimeout(function () {
resolve(data)
}, 0);
})
}
function getUserMessages (userId) {
return new Promise(function(resolve, reject){
var data = [{text: 'hello'}, {text: 'Do you like music?'}, {text: 'Numbers are cool.'}]
//setTimeout roughly simulates an HTTP request.
setTimeout(function () {
resolve(data)
}, 0)
});
}
function getUserLocation (userId) {
return new Promise(function(resolve, reject){
var data = {state: 'CO', city: 'Denver'}
//setTimeout roughly simulates an HTTP request.
setTimeout(function () {
resolve(data)
}, 0)
});
}
function renderData (user, messages, location) {
console.log(user, messages, location)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment