如何在node.js和express.js中创建非阻塞异步函数
我通过WebMatrix创建了express.js示例。我想创建一个api,从myfunction中获取结果。如果第一个请求案例复杂且花费大量时间,而第二个请求案例简单,则第二个请求必须等待第一个请求完成。我是否可以做一些事情,使第二个请求返回数据的速度比第一个请求快如何在node.js和express.js中创建非阻塞异步函数,node.js,express,Node.js,Express,我通过WebMatrix创建了express.js示例。我想创建一个api,从myfunction中获取结果。如果第一个请求案例复杂且花费大量时间,而第二个请求案例简单,则第二个请求必须等待第一个请求完成。我是否可以做一些事情,使第二个请求返回数据的速度比第一个请求快 app.post('/getData', function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-
app.post('/getData', function(req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
var case= req.body.case;
var json = myfunction(case);
res.json(json);
});
您可以使用async来实现这一点:
var async = require('async');
async.waterfall([
function(callback){ // first task
// process myCase (don't use case, it's reserved word),
// then pass it to your next function
callback(null, myCase); // 1st argument: null means no error
// if error, pass error in 1st arg
// so that 2nd function won't be
// executed
},
function(myCase, callback){ // 2nd task
// use argument 'myCase' to populate your final json result (json)
// and pass the result down in the callback
callback(null, json);
}
], function (err, json) {
// the argument json is your result
res.json(json);
});
您可以使用async来实现这一点:
var async = require('async');
async.waterfall([
function(callback){ // first task
// process myCase (don't use case, it's reserved word),
// then pass it to your next function
callback(null, myCase); // 1st argument: null means no error
// if error, pass error in 1st arg
// so that 2nd function won't be
// executed
},
function(myCase, callback){ // 2nd task
// use argument 'myCase' to populate your final json result (json)
// and pass the result down in the callback
callback(null, json);
}
], function (err, json) {
// the argument json is your result
res.json(json);
});
如果您愿意,您不必使用任何外部库。例如,您可以执行以下操作:
console.log('1');
function async(input, callback) {
setTimeout(function() {
//do stuff here
for (var i = 0; i < input; i++) {
//this takes some time
}
//call callback, it may of course return something
callback('2');
}, 0);
}
async('10000000', function(result) {
console.log(result);
});
console.log('3');
console.log('1');
函数异步(输入、回调){
setTimeout(函数(){
//在这里做事
对于(变量i=0;i
您可以测试它,并看到“2”将是1和3之后的打印机。
希望有帮助
PS您也可以使用setInterval或下划线库:
var _ = require('underscore');
console.log('1');
function async(input, callback) {
_.defer(function() {
//do stuff here, for ie - this time-consuming loop
for (var i = 0; i < input; i++) {
//this takes some time
}
//call callback, it may of course return something
callback('2');
});
}
async('10000000', function(result) {
console.log(result);
});
console.log('3');
var\=require('下划线');
console.log('1');
函数异步(输入、回调){
_.defer(函数(){
//在这里做一些事情,例如,这个耗时的循环
对于(变量i=0;i
如果您愿意,您不必使用任何外部库。例如,您可以执行以下操作:
console.log('1');
function async(input, callback) {
setTimeout(function() {
//do stuff here
for (var i = 0; i < input; i++) {
//this takes some time
}
//call callback, it may of course return something
callback('2');
}, 0);
}
async('10000000', function(result) {
console.log(result);
});
console.log('3');
console.log('1');
函数异步(输入、回调){
setTimeout(函数(){
//在这里做事
对于(变量i=0;i
您可以测试它,并看到“2”将是1和3之后的打印机。
希望有帮助
PS您也可以使用setInterval或下划线库:
var _ = require('underscore');
console.log('1');
function async(input, callback) {
_.defer(function() {
//do stuff here, for ie - this time-consuming loop
for (var i = 0; i < input; i++) {
//this takes some time
}
//call callback, it may of course return something
callback('2');
});
}
async('10000000', function(result) {
console.log(result);
});
console.log('3');
var\=require('下划线');
console.log('1');
函数异步(输入、回调){
_.defer(函数(){
//在这里做一些事情,例如,这个耗时的循环
对于(变量i=0;i
case不是保留字吗?看看库。我建议您查看-根据简单请求和harh请求划分逻辑并创建一些子进程。case
不是保留字吗?看看库。我建议您查看-根据简单请求和harh请求划分逻辑并创建一些子进程
。