gpt4 book ai didi

javascript - Q.all 没有以正确的顺序解析

转载 作者:行者123 更新时间:2023-11-30 17:23:40 24 4
gpt4 key购买 nike

我在 Node.js 中遇到 Q promise 的执行顺序问题。

代码应该执行以下操作:

a) 执行查询并使用生成的纬度/经度对

b) 计算最短路径(使用异步函数)和

c) 归还它们

  query() // Runs the query
.then(function() {
return computeRoutes(); // Calculates the routes
})
.then(function() {
return returnRoutes(); // Returns the routes as JSON
});

问题是,虽然 coordinates[] 数组在 query() 中填充并在 computeRoutes() 中填充/可用,但 routes[] 数组在 returnRoutes() 中保持为空。

奇怪的是,当我没有在 computeRoutes() 中遍历坐标 [] 而是只为坐标 [0] 计算一条路线时, promise 链成功(但当然我只看到一条路线作为 JSON 返回)

对这里可能出现的问题有什么想法吗?

谢谢!!

完整代码:

app.get('/',
function(req, res) {

var id = parseInt(req.query.id);
var radius = parseInt(req.query.radius) || 5000;
var routes = [];
var coordinates = [];

function query() {
var deferred = Q.defer();

console.log('Starting query function...');
var query = client.query('SELECT ST_X(ST_Transform(ST_SetSRID(a.geom, 3857),4326)) AS fromlat, ST_Y(ST_Transform(ST_SetSRID(a.geom, 3857),4326)) AS fromlon, ST_X(ST_Transform(ST_SetSRID(b.geom, 3857),4326)) AS tolat, ST_Y(ST_Transform(ST_SetSRID(b.geom, 3857),4326)) AS tolon FROM emme_veh AS c, emme_nodes3857 AS b, emme_nodes3857 AS a WHERE c.fid = a.id AND c.tid = b.id AND c.fid = $1 AND ST_Distance(a.geom, b.geom) < $2', [id, radius], function(err, result) {
console.log('Inside query. result.rows.length:',result.rows.length);

for(var i in result.rows) {
coordinates.push({'from':[result.rows[i].fromlon,result.rows[i].fromlat], 'to':[result.rows[i].tolon,result.rows[i].tolat]});
}

deferred.resolve();
});
return deferred.promise;
}

function computeRoutes() {
var the_promises = [];

for(var i in coordinates) {
var deferred = Q.defer();

var query = {coordinates: [coordinates[i].from, coordinates[i].to], alternateRoute: false}
osrm.route(query, function(err, result) {
if(err) return res.json({"error": err.message});
// console.log(result.route_geometry);
routes.push(result.route_geometry);
deferred.resolve();
});
the_promises.push(deferred.promise);
}
return Q.all(the_promises);
}

function returnRoutes() {
return res.json(routes);
}

query()
.then(function() {
return computeRoutes();
})
.then(function() {
return returnRoutes();
});

});

最佳答案

经过反复试验找到了答案。

问题是我使用 for..in 循环在 computeRoutes() 函数中生成 promise 。

切换到带有函数的 forEach 循环就可以了。

工作代码片段(查看 computeRoutes() 部分):

app.get('/',
function(req, res) {

var id = parseInt(req.query.id);
var radius = parseInt(req.query.radius) || 5000;
var routes = [];
var coordinates = [];

function query() {
var deferred = Q.defer();

console.log('Starting query function...');
var query = client.query('SELECT ST_X(ST_Transform(ST_SetSRID(a.geom, 3857),4326)) AS fromlat, ST_Y(ST_Transform(ST_SetSRID(a.geom, 3857),4326)) AS fromlon, ST_X(ST_Transform(ST_SetSRID(b.geom, 3857),4326)) AS tolat, ST_Y(ST_Transform(ST_SetSRID(b.geom, 3857),4326)) AS tolon FROM emme_veh AS c, emme_nodes3857 AS b, emme_nodes3857 AS a WHERE c.fid = a.id AND c.tid = b.id AND c.fid = $1 AND ST_Distance(a.geom, b.geom) < $2', [id, radius], function(err, result) {
console.log('Inside query. result.rows.length:',result.rows.length);

for(var i in result.rows) {
coordinates.push({'from':[result.rows[i].fromlon,result.rows[i].fromlat], 'to':[result.rows[i].tolon,result.rows[i].tolat]});
}

deferred.resolve();
});
return deferred.promise;
}

function computeRoutes() {
var the_promises = [];

console.log('Inside computeRoutes()');

coordinates.forEach(function(coordinate) {
var deferred = Q.defer();

osrm.route({coordinates: [coordinate.from, coordinate.to], alternateRoute: false}, function(err, result) {
deferred.resolve(result);
routes.push(result.route_geometry);
});
the_promises.push(deferred.promise);
});

return Q.all(the_promises);
}

function returnRoutes() {
console.log('Inside returnRoutes()');
return res.json(routes);
}

query()
.then(function() {
console.log('then() 1');
return computeRoutes();
})
.then(function() {
console.log('then() 2');
return returnRoutes();
});

关于javascript - Q.all 没有以正确的顺序解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com