Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Forked from rmurphey/original.js
Created September 17, 2012 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/3735267 to your computer and use it in GitHub Desktop.
Save ryanflorence/3735267 to your computer and use it in GitHub Desktop.
function getSomeThings(callback) {
var completed = 0;
var people, tasks;
$.ajax('/data/people.json', {
dataType: 'json',
success: function(data) {
completed++;
people = data.people;
onFinished();
}
});
$.ajax('/data/tasks.json', {
dataType: 'json',
success: function(data) {
completed++;
tasks = data.tasks;
onFinished();
}
});
function onFinished() {
if (completed !== 2) { return; }
callback(people, tasks);
}
}
function getSomeThings(callback) {
var peopleRequest = $.getJSON('/data/people.json').then(function(data) {
return data.people;
});
var tasksRequest = $.getJSON('/data/tasks.json').then(function(data) {
return data.tasks;
});
$.when(peopleRequest, tasksRequest).then(callback);
}
function getSomeThings(callback) {
var reqs = $.map([ 'people', 'tasks' ], function(idx, req) {
return $.getJSON('/data/' + req + '.json').then(function(data) {
return data[req];
});
});
$.when.apply($, reqs).then(callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment