Created
August 22, 2012 17:00
-
-
Save luiselizondo/3427508 to your computer and use it in GitHub Desktop.
nodejs-async-blocks-example-async
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
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} | |
}); |
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(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) | |
}) | |
}) | |
} |
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
$.get("http://example.com/someExtraData", function(data) { | |
// Do something with your data like: | |
$("ul").append("<li>" + data.title +"</li>"); | |
}) |
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
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