Ich habe ein Problem mit Versprechen in NodeJS sogar dachte ich bin mit Bluebird. Im folgenden Code:Promised Cicle innerhalb Promisified Cicle in NodeJS
var Promise = require('bluebird');
var google = require('utils/google');
// list of (max 60 * number of types defined) nearby places
var nearby_places = [];
Promise.each(config.type.nearby, function (type) {
google.nearbySearch(request.query.lat, request.query.lng, request.query.rad, type)
.then(function (places) {
// list of (max 60) places
var detailed_places = [];
console.log('Found ' + places.length + ' places.');
// Get full details of places and insert them into database
Promise.each(places, function (place_id) {
google.getPlaceDetails(place_id)
.then(function (res) {
detailed_places.push(res);
})
.catch(function (err) {
console.log(err);
});
})
.then(function() {
// Push detailed places to nearby places
nearby_places = nearby_places.concat(detailed_places);
});
})
.catch(function (err) {
console.log(err);
});
}).then(function() {
// Returns (max 60 * number of types defined) nearby places
return reply(nearby_places);
});
Es führt den google.nearbySearch()
und Druck Somethings dort, so dass ich weiß, dass es läuft. Aber wenn es endet, wartet es nicht auf das Innere Promise.each(places, ...
. Es geht direkt auf die return reply(...
mit nearby_places
leer. Dann zeigt es mir in der Konsole .
Ich glaube nicht, ich verstehe Versprechen/Bluebird, aber ich weiß nicht, was ich falsch mache.
Sie müssen möglicherweise auch paraphieren zu '' return' google.getPlaceDetails (place_id) '' aus .each() 'call, _" Wenn die Iterator-Funktion eine Zusage oder eine thinable zurückgibt, wird das Ergebnis der Zusage abgewartet, bevor mit der nächsten Iteration fortgefahren wird. "_ http://bluebirdjs.com/docs/api/promise.each.html – guest271314
Ich musste auch google.nearbySearch (... 'zusätzlich zu Ihren Antworten zurückgeben. Danke! – Illysis