如何可以处理多个callback在nodejs返回值?

我试图执行基于callback的结果,如果条件,但我无法写代码的SQL查询。所以请在代码中提供索姆信息

app.get('/resell-property', function(req, res) { var data = {} data.unit_price_id = 1; function callback(error, result) { if (result.count == 0) { return hp_property_sell_request.create(data) } else if (result.count > 0) { return hp_unit_price.findAll({ where: { unit_price_id: data.unit_price_id, hp_property_id: data.property_id, hp_unit_details_id: data.unit_details_id } }) } } hp_property_sell_request.findAndCountAll({ where: { unit_price_id: data.unit_price_id } }).then(function (result) { if (result) { callback(null, result); } }); }); 

在这个我怎么能写callback

 hp_property_sell_request.create(data) ,hp_unit_price.findAll({ where: { unit_price_id: data.unit_price_id, hp_property_id: data.property_id, hp_unit_details_id: data.unit_details_id } }) 

在返回结果后,我必须处理callback和执行此查询

 if(result.request_id){ return hp_unit_price.findAll({ where:{ unit_price_id:result.unit_price_id, hp_property_id:result.property_id, hp_unit_details_id:result.unit_details_id } }).then(function (result){ if(result.is_resale_unit==0 && result.sold_out==0){ return Sequelize.query('UPDATE hp_unit_price SET resale_unit_status=1 WHERE hp_unit_details_id='+result.unit_details_id+' and hp_property_id='+result.property_id) } }) } 

promiseparsing函数只需要一个input参数,所以如果你需要传入多个东西,你必须把它们放在一个单独的对象中。 就像,如果你必须像这样的东西:

 database.openCollection() .then(function(collection){ var result = collection.query(something); var resultObject = { result: result, collection: collection }; }) .then(function(resultObject){ doSomethingSyncronousWithResult(resultObject.result); resultObject.collection.close(); }); 

如果你所有的东西都不是诺言解决的结果,你就不能使用诺言,你可能需要这样的东西。

免责声明:代码示例是非常差的,但它解释了这个概念。

我build议你学习诺言,特别是蓝鸟。 您可以promisify传统的callback方法。

我也将在不同的文件中创build模型级别的function。 这是一个例子。

parent.js

 const db = require("./connections/database"); // connection to database const getChildForParent = function (parentId, childId, callback) { db.find({parent: parentId, child_id: childId}, "childrenTable", function(err, result) { if (err) { return callback(err); } return callback(null, result); }); }; 

children.js

 const db = require("./connections/database"); // connection to database const getToysForChild = function (childId, callback) { db.find({toy_belongs_to: parentId}, "toysTable", function(err, result) { if (err) { return callback(err); } return callback(null, result); }); }; 

然后在控制器中,你可以做这样的事情:

 const Bluebird = require("bluebird"); const Parent = require("./parent.js"); const Child = require("./child.js"); // Promisifying adds "Async" at the end of your methods' names (these are promisified) Bluebird.promisifyAll(Parent); Bluebird.promisifyAll(Child); // Just an example. app.get("/parent/:parentId/children/:childId", function(req, res) { return Bluebird.try(function() { return User.getChildForParentAsync(req.params.parentId, req.params.childId); }).then(function(child) { return Child.getToysForChildAsync(child.child_id); }).then(function(toys) { // Do something with toys. }); }); 

当然,你可以做更多的事情,这不是唯一的方法。

你也可以使用Promise.all() 。 当您想要等待多个承诺完成时,此方法非常有用。

比方说,你有一个url列表,你想要获取并处理所有的数据已被提取的结果。

 var urls = [url1, url2, url3, url4, url5 .......... ]; var Bluebird = require("bluebird"); var request = require("request"); // callback version library Bluebird.promisifyAll(request); // create a list which will keep all the promises var promises = []; urls.forEach(function(url) { promises.push(request.getAsync(url1)); }); // promises array has all the promises // Then define what you want to do on completion. Bluebird.all(promises).then(function(results) { // results is an array with result a url in an index // process results. }); 

我会build议使用Promise来解决这个问题。 如果你需要所有请求的所有结果,当他们都完成Promise.all()会为你做。 你的基本可能是这样的:

 var req1 = new Promise(function(res, rej){ var req = new XMLHttpRequest() … req.addEventListener('load', function (e) { res(e); }) var req2 = //similar to the above Promise.all([req1, req2, …]).then(function(values){ //all requests are done here and you can do your stuff }); 

你也可以使用新的获取API,创buildPromise,如下所示:

 var req1 = fetch(…); var req2 = fetch(…); Promise.all([req1, re2, …]).then(…);