Skip to content

Instantly share code, notes, and snippets.

@luiselizondo
Created August 22, 2012 17:00
Show Gist options
  • Save luiselizondo/3427508 to your computer and use it in GitHub Desktop.
Save luiselizondo/3427508 to your computer and use it in GitHub Desktop.
nodejs-async-blocks-example-async
async.series({
somedata: function(callback){
// query the database to get "somedata" and pass the result to callback
},
someextradata: function(callback){
// query the database to get the "someextradata" and pass the result to callback
},
},
function(err, results) {
// results is now equal to: {somedata: yourData, someextradata: yourExtraData}
});
function getData(id, callback) {
db.blogs.find(function(error, result) {
var someData = result;
db.somethingElse.find(function(err, result) {
var someExtraData = result;
var results = {
someData: someData,
someExtraData: someExtraData
}
callback(error, results)
})
})
}
$.get("http://example.com/someExtraData", function(data) {
// Do something with your data like:
$("ul").append("<li>" + data.title +"</li>");
})
app.get("/someExtraData", function(req, res) {
var someExtraData = { // This will come from the database }
res.json(someExtraData)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment