function DatabaseConnect(req, res) {...}
function CreateNewUser(req, res) {...}
function Go (app, req, res) {
// If is POST on this URL run this function
var firstFunction = app.post('/admin/svc/DB', function(res, req) {
DatabaseConnect(req, res)
});
// If is POST on this URL run this function
var secondFunction = app.post('/admin/svc/CREATE', function(res, req) {
CreateNewUser(req, res)
});
// Run first all callbacks and after render page
function Render(firstMe, afterMe) {
firstMe();
afterMe();
app.render('screen');
}
Render(firstFunction, secondFunction);
}
Go();
如何运行更多功能asyn。和Render()毕竟?
如果该URI上的ist POST,则调用APP.POST。
答案 0 :(得分:0)
根据您的需要,这里有各种不同的方法。如果你只关心完成一定数量的异步函数,一个常见的模式是基本上保持计数并在你完成后触发afterAll()
方法:
var count = 0,
target = 2; // the number of async functions you're running
function afterAll() {
if (++count === target) {
// do something
}
}
doAsync1(function() {
// some stuff
afterAll();
});
doAsync2(function() {
// some stuff
afterAll();
});
Underscore在此处提供了一些有用的实用程序,包括_.after
:
var asyncMethods = [doAsync1, doAsync2, doAsync3],
// set up the callback to only run on invocation #3
complete = _.after(asyncMethods.length, function() {
// do stuff
});
// run all the methods
asyncMethods.forEach(function(method) {
// this assumes each method takes a callback
method(complete);
});