Created
April 23, 2014 02:01
-
-
Save asselin/11200509 to your computer and use it in GitHub Desktop.
promises
This file contains 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
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 | |
} | |
); | |
} |
This file contains 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
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 | |
} | |
); | |
} |
This file contains 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
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