Javascript Ajax调用在执行所有节点回调之前完成

Javascript Ajax调用在执行所有节点回调之前完成,javascript,jquery,ajax,node.js,callback,Javascript,Jquery,Ajax,Node.js,Callback,我试图对节点服务器进行ajax post调用,然后在调用完成后重定向用户。下面是我的jquery函数,用于进行post ajax调用 $.ajax({ url: '/myUrl', type: 'POST', data: { myParam: myParam,fileName: finalName}, success: function(data, status){ alert('Ajax call completed!'); customFunction(st

我试图对节点服务器进行ajax post调用,然后在调用完成后重定向用户。下面是我的jquery函数,用于进行post ajax调用

$.ajax({
  url: '/myUrl', 
  type: 'POST',
  data: { myParam: myParam,fileName: finalName},
  success: function(data, status){
    alert('Ajax call completed!');
    customFunction(status);

  }, 
  error: function(xOptions, textStatus){
    alert('Error occured!: '+textStatus);
  }
});
现在,在服务器端,我以如下方式处理此请求:

var modCust = require('./custom-module')

app.post('/myUrl',function(req,res){
  var myParam = req.body.myParam;
  var fileName = req.body.fileName;

  var cli = modCust.parseExcel(fileName,myParam);
  res.send(cli);
});
此自定义模块使用嵌套回调执行多个db函数,如下所示:

//#custom-module.js

executeQuery = function(strSQL, operationType, tableName, cb,myParam) {
    var request = new sql.Request(connection);
    request.query(strSQL,function(err, recordset) {
        if(err){
            console.error('ERROR in '+operationType+' ON '+tableName+': '+err);
        }
        console.info(operationType+' ON '+tableName+' successful!');
        if(cb){
            cb(myParam);
        }

        return recordset;
    });
};

parseFile: function(filePath, myParam){
  var strSQL = "<sample query>";
  executeQuery(strSQL,'<QueryType>','<table_name>',processDB1,myParam);
},

processDB1: function(myParam){
  var strSQL = "<sample query>";
  executeQuery(strSQL,'<QueryType>','<table_name>',processDB2,myParam);
},

processDB2: function(myParam){
  var strSQL = "<sample query>";
  executeQuery(strSQL,'<QueryType>','<table_name>',processDB3,myParam);
},

processDB3: function(myParam){
  var strSQL = "<sample query>";
  executeQuery(strSQL,'<QueryType>','<table_name>');
},
/#custom-module.js
executeQuery=函数(strSQL、operationType、tableName、cb、myParam){
var request=newsql.request(连接);
查询(strSQL,函数(err,记录集){
如果(错误){
console.error(“+tableName+”上的“+operationType+”中出现错误:”+err);
}
console.info(operationType+'ON'+tableName+'successful!');
如果(cb){
cb(myParam);
}
返回记录集;
});
};
parseFile:函数(filePath,myParam){
var strSQL=“”;
executeQuery(strSQL,,'',processDB1,myParam);
},
processDB1:函数(myParam){
var strSQL=“”;
executeQuery(strSQL,,'',processDB2,myParam);
},
processDB2:函数(myParam){
var strSQL=“”;
executeQuery(strSQL,,'',processDB3,myParam);
},
processDB3:函数(myParam){
var strSQL=“”;
执行(strSQL,,'';
},
但问题是,在processDB3()之前,警报调用甚至在所有回调完成之前执行。我能否仅在从节点完成最后一次回调后弹出警报


谢谢

您需要查看javascript中的承诺。函数parseExcel应返回promise,以便您可以执行以下操作:

modCust.parseExcel(fileName,myParam).then(function(resultFromParseExcelFunction) {
    res.send(resultFromParseExcelFunction)
})
检查一下,以轻松的方式实现承诺