Q.all 块中的所有函数都没有被承诺
All functions in Q.all block are not being promised
我在下面的 then 块中有以下代码
我面临的问题是在我执行 res.json(optionData1) 时,它不是 return 完全完成的 js 数据对象,即缺少 processData 函数后的输出
我使用 Q.all 的方式是否正确?
var processUserInfo = function(categoryToProcess, inputToProcess, optionComingIn) {
var d = Q.defer();
if (optionData1['option'] == optionComingIn) {
if (optionData1[categoryToProcess].hasOwnProperty(inputToProcess)) {
optionData1[categoryToProcess][inputToProcess]++;
} else {
optionData1[categoryToProcess][inputToProcess] = 1;
}
d.resolve(optionData1);
}
}
var processData = function(item, optionComingIn) {
var d = Q.defer();
return User.find(
{_id: item},
{gender: 1, country:1},
function(req, foundUser) {
processUserInfo('gender', foundUser[0]['gender'], optionComingIn)
.then(function(resolve,reject) {
d.resolve();
});
});
return d.promise;
}
Q.all(foundQ[0]['people'].map(function(item) { // Or Q.allSettled
processCounts(item['optionSelected']);
processData(item['userID'], item['optionSelected']);
}))
.then(function(){
res.json(optionData1); //Doesnt give me the full result
});
谢谢
更新:使用下面答案中的 return 方法使一切正常。
这是可能有效的代码 - 您的代码片段中有太多 "unknown" 无法确定
- 将
processData
修改为 return 当 user.Find
完成时解析的承诺
- 在
.map
中添加了一个 return,因此由 processData
编辑的承诺 return 在 Q.all
中等待
所以...这是固定代码(processuserInfo 未更改,因此从答案中省略)
var processData = function (item, optionComingIn) {
// return a promise to wait for
return Q.promise(function(resolve, reject) {
User.find({
_id: item
}, {
gender: 1,
country: 1
},
function (req, foundUser) {
processUserInfo('gender', foundUser[0]['gender'], optionComingIn);
resolve();
}
);
});
}
Q.all(foundQ[0]['people'].map(function (item) { // Or Q.allSettled
processCounts(item['optionSelected']);
return processData(item['userID'], item['optionSelected']);
// return added
}))
.then(function () {
res.json(optionData1); //Doesnt give me the full result
});
我在下面的 then 块中有以下代码
我面临的问题是在我执行 res.json(optionData1) 时,它不是 return 完全完成的 js 数据对象,即缺少 processData 函数后的输出
我使用 Q.all 的方式是否正确?
var processUserInfo = function(categoryToProcess, inputToProcess, optionComingIn) {
var d = Q.defer();
if (optionData1['option'] == optionComingIn) {
if (optionData1[categoryToProcess].hasOwnProperty(inputToProcess)) {
optionData1[categoryToProcess][inputToProcess]++;
} else {
optionData1[categoryToProcess][inputToProcess] = 1;
}
d.resolve(optionData1);
}
}
var processData = function(item, optionComingIn) {
var d = Q.defer();
return User.find(
{_id: item},
{gender: 1, country:1},
function(req, foundUser) {
processUserInfo('gender', foundUser[0]['gender'], optionComingIn)
.then(function(resolve,reject) {
d.resolve();
});
});
return d.promise;
}
Q.all(foundQ[0]['people'].map(function(item) { // Or Q.allSettled
processCounts(item['optionSelected']);
processData(item['userID'], item['optionSelected']);
}))
.then(function(){
res.json(optionData1); //Doesnt give me the full result
});
谢谢
更新:使用下面答案中的 return 方法使一切正常。
这是可能有效的代码 - 您的代码片段中有太多 "unknown" 无法确定
- 将
processData
修改为 return 当user.Find
完成时解析的承诺 - 在
.map
中添加了一个 return,因此由processData
编辑的承诺 return 在Q.all
中等待
所以...这是固定代码(processuserInfo 未更改,因此从答案中省略)
var processData = function (item, optionComingIn) {
// return a promise to wait for
return Q.promise(function(resolve, reject) {
User.find({
_id: item
}, {
gender: 1,
country: 1
},
function (req, foundUser) {
processUserInfo('gender', foundUser[0]['gender'], optionComingIn);
resolve();
}
);
});
}
Q.all(foundQ[0]['people'].map(function (item) { // Or Q.allSettled
processCounts(item['optionSelected']);
return processData(item['userID'], item['optionSelected']);
// return added
}))
.then(function () {
res.json(optionData1); //Doesnt give me the full result
});