Skip to content

Instantly share code, notes, and snippets.

@asselin
Created April 23, 2014 02:01
Show Gist options
  • Save asselin/11200509 to your computer and use it in GitHub Desktop.
Save asselin/11200509 to your computer and use it in GitHub Desktop.
promises
function getData(callback) {
var result = {};
getJSONAsync('/users',
function(data) {
result.users = data;
getJSONAsync('/companies',
function(data) {
result.companies = data;
getJSONAsync('/contacts',
function(data) {
result.contacts = data;
callback(result);
},
function(error) {
// error handler
}
);
},
function(error) {
// error handler
}
);
},
function(error) {
// error handler
}
);
}
function getData() {
var result = {};
return getJSON('/users').then(
function(data) {
result.users = data;
return getJSON('/contacts');
}
).then(
function(data) {
result.companies = data;
return getJSON('/companies');
}
).then(
function(data) {
result.contacts = data;
return result;
}
).then(
null,
function() {
// error handler
}
);
}
function getData() {
var result = {};
try {
result.users = getJSON('/users');
result.companies = getJSON('/companies');
result.contacts = getJSON('/contacts');
return result;
} catch (e) {
// error handler
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment