[英]Recursion, Node js and Asynchronous calls
I am trying to get the savePlaces array as the final output so that I can return it in the response. 我试图将savePlaces数组作为最终输出,以便可以在响应中返回它。 But I am getting success as undefined in function2().
但是我在function2()中获得了未定义的成功。 Why I am not getting the savePlaces as expected.
为什么我没有得到所期望的savePlaces。 Please provide solution....
请提供解决方案。
var saveOverriddenPlaces = function(index, savePlaces) {
var groupSettingsDAO = new GroupSettingsDAO();
var deferred = q.defer();
if (index == savePlaces.length) {
console.log("-----------------------------deferred--------------------------------", deferred);
console.log("LAST RETURN -----------------------------" + savePlaces + "----------------------------------------------------", index);
deferred.resolve(savePlaces);
return deferred.promise;
}
var placeInfo = {
"id": savePlaces[index].id,
"groupurl": savePlaces[index].groupurl,
"sla": savePlaces[index].sla
};
if (savePlaces[index]._id) {
placeInfo._id = savePlaces[index]._id;
//update the overriden places in the database
groupSettingsDAO.updateOverriddenPlace(placeInfo)
//updates the overriden place and sends result in response when successful else logs error
.then(
//success
function(success) {
console.log("recursion ============" + index + "=======================");
deferred.resolve(saveOverriddenPlaces(++index, savePlaces));
return deferred.promise;
},
//failure
function(error) {
console.log("PLACES ERROR ===================================", error);
});
// placesWithID.push(value);
}
//WITHOUT ids
else {
placeInfo._id = guid();
savePlaces[index]._id = placeInfo._id;
groupSettingsDAO.saveOverriddenPlace(placeInfo)
// saves the overridden place and sends the results in response if successful else logs the error
.then(
//success
function(success) {
console.log("recursion ============" + index + "=======================");
deferred.resolve(saveOverriddenPlaces(++index, savePlaces));
},
//failure
function(error) {
console.log("PLACES ERROR ===================================", error);
});
}
}
function2(req, res) {
saveOverriddenPlaces(0, req.savePlaces).then(function(success) {
//getting success as undefined
res.send({
"result": success
});
});
}
This is because saveOverriddenPlaces
is not returning a value and therefore it is not passed to the .then
statement in function two. 这是因为
saveOverriddenPlaces
没有返回值,因此它没有传递给函数2中的.then
语句。 The "solution" is to make sure that saveOverridenPlaces
returns a Promise that calls resolve with a value that you want to pass to success. “解决方案”是确保
saveOverridenPlaces
返回一个Promise,该Promise调用您要传递给成功的值的resolve。
The problem is that in the line 问题在于
deferred.resolve(saveOverriddenPlaces(++index, savePlaces));
your saveOverriddenPlaces
function does not (always) return a promise or a value. 您的
saveOverriddenPlaces
函数不会(总是)返回承诺或值。 If a function is asynchronous, it must always return a promise. 如果一个函数是异步的,则它必须始终返回一个promise。
You can easily do that by adding a return
before the groupSettingsDAO.…().then(…)
calls in your conditional statements - as then
does return exactly the promise that you want if you return
a value/prmise from its callback. 您可以通过在条件语句的
groupSettingsDAO.…().then(…)
调用之前添加return
来轻松实现此操作- then
,如果从其回调中return
值/承诺, then
确实会返回您想要的承诺。
However, you should try to avoid the deferred antipattern . 但是,您应尽量避免延迟使用反模式 。 For your base case, use the
Q
function to create a fulfilled promise, for everything else just chain then
calls - no deferreds needed. 对于您的基本情况,使用
Q
函数创建一个已兑现的诺言,对于其他所有内容,只需链接then
调用-无需延迟。
function saveOverriddenPlaces(index, savePlaces) {
if (index == savePlaces.length) {
console.log("LAST RETURN -" + savePlaces + "-", index);
return Q(savePlaces);
}
var groupSettingsDAO = new GroupSettingsDAO();
var placeInfo = {
"id": savePlaces[index].id,
"groupurl": savePlaces[index].groupurl,
"sla": savePlaces[index].sla
};
var promise;
if (savePlaces[index]._id) {
placeInfo._id = savePlaces[index]._id;
promise = groupSettingsDAO.updateOverriddenPlace(placeInfo); //update the overriden places in the database
// placesWithID.push(value);
} else { // WITHOUT ids
placeInfo._id = guid();
savePlaces[index]._id = placeInfo._id;
promise = groupSettingsDAO.saveOverriddenPlace(placeInfo) // saves the overridden place
}
return promise.then(function(success) {
// ^^^^^^
console.log("recursion =" + index + "=" + success);
return saveOverriddenPlaces(++index, savePlaces));
// ^^^^^^
});
}
function2(req, res) {
saveOverriddenPlaces(0, req.savePlaces).then(function(success) {
//getting success as undefined
res.send({
"result": success
});
}, function(error) {
console.log("PLACES ERROR =", error);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.