NodeJS异步函数.怎么做A, B,然后C
NodeJS asynchronous functions. How to do A and B then C?
我编写了以下代码,并在mongo控制台上成功地进行了测试。
var up_results = db.upParts.find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}},{"_id":1, "features.properties.partID":1,"features.properties.connectedTo":1}).toArray();
var down_results = db.downParts.find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray();
var part_results = [];
for (i = 0; i < up_results.length; i++) {
for (j = 0; j < up_results[i].features[0].properties.connectedTo.length; j++) {
for (k = 0; k < down_results.length; k++) {
if (down_results[k]._id == up_results[i].features[0].properties.connectedTo[j]) {
part_results.push(db.parts.find({_id:{$in:[ObjectId(up_results[i].features[0].properties.partID)]}}));
}
}
}
}
parts_results.length;
我现在试图实现它在nodejs…但我觉得我做得不对
我是这样开始的:
var up_results = null;
var down_results = null;
var part_results = [];
function geoqueries(callback) {
self.db.collection('upParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}}, {"_id": 1, "features.properties.partID": 1, "features.properties.connectedTo": 1}).toArray(function (err, document) {
up_results = document;
});
self.db.collection('downParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray(function(err, document2) {
down_results = document2;
});
callback();
}
function dosomething() {
...do something with up_results and down_results
}
geoqueries(dosomething);
如何告诉geoqueries() upParts和downParts查找查询已经完成?
尝试使用Promises。
http://howtonode.org/promises是关于。的最佳资源之一。
你可以做一连串的承诺。比如:
var promises = [];
var deferred = q.defer();
promises.push(deferred.promise);
self.db.collection('upParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [3, 5]}}}}, {"_id": 1, "features.properties.partID": 1, "features.properties.connectedTo": 1}).toArray(function (err, document) {
up_results = document;
deferred.resolve();
});
var deferred_i = q.defer();
promises.push(deferred_i.promise);
self.db.collection('downParts').find({"features.geometry": {$geoIntersects: {$geometry: {type: "Point", coordinates: [-80, 30]}}}},{"_id":1, "features.properties.partID":1}).toArray(function(err, document2) {
down_results = document2;
deferred_i.resolve();
});
q.all(promises)
.then(function() {
callback();
});
相关文章:
- 在每个循环上完成异步调用,然后转到下一个节点 js
- 从异步调用返回数组,然后为数组的每个元素返回其他异步调用
- 异步加载ASP.NET绑定的Javascript,然后调用回调
- 单击链接时执行异步AJAX调用,然后跟随该链接
- 请等待异步操作 (firebase) 调用完成,然后再加载网页
- 如何在 $ 中执行异步操作.延迟,然后失败过滤回调并传递回成功链
- Jasmine - 等待异步库完全加载,然后再执行测试
- 加载异步 JS 然后调用函数
- 如何使用jQuery Deferred(何时/然后等)来修复末日问题的异步金字塔
- ExtJS 4拖放等待异步操作,然后再决定是否完成拖放
- Node.js:执行多个异步操作的最佳方式,然后执行其他操作
- 链接多个.然后,一些没有异步代码的函数.最佳实践
- 在RequireJS中使用异步加载来定义一个模块,然后在immediate函数中使用它安全吗?
- 主干创建集合,然后视图-异步
- NodeJS异步函数.怎么做A, B,然后C
- 承诺:如何并行执行异步方法,然后执行方法
- webdriver.IO异步问题-点击一个元素,然后输入文本
- 异步获取函数上生成的数组,然后开箱即用地传递它
- 正在等待jquery异步加载,然后再加载js
- Node.js等待直到异步函数完成,然后继续执行其余的代码