我正在做以下代码:
let promises = []
querySnapshot.forEach(function (doc) {
promises.push(
promiseFunction()
.then((postCounters) => {
promiseFunction2()
.then((myRate) => {
console.log('save array with postCounters AND myRate data')
})
})
)
})
Promise.all(promises).then(() => {
console.log('the array with postCounters AND myRate data')
})
事情出了问题。返回的Promise.all不会等到promiseFunction2运行,所以第二个函数不工作,我没有得到我的数组中的promiseFunction2数据,只是第一个。
我认为发生的事情是:Promise.all在所有promiseFunction运行之后都会得到,但是它不会等到第二个函数,我想等到完全答应之前完成,我应该使用两个Promise 。所有???
我不太熟悉Promise.all的语法,我只是使用promiseFunction()。then()方法
(使我的代码保持一致以便扩展的事件)
答案 0 :(得分:2)
您应该使用map
将一个数组转换为另一个数组。问题是你没有返回promiseFunction2生成的promise链:
const promises = querySnapshot.map((doc) => (
promiseFunction()
.then((postCounters) => (
promiseFunction2()
.then((myRate) => {
console.log('save array with postCounters AND myRate data')
})
))
));
Promise.all(promises).then(() => {
console.log('the array with postCounters AND myRate data')
});
或者,对于更少的缩进噪音,请使用async/await
:
const promises = querySnapshot.map(async (doc) => {
const postCounters = await promiseFunction();
const myRate = await promiseFunction2();
// do stuff with myRate
console.log('save array with postCounters AND myRate data')
// async functions automatically return promises that resolve when the block finishes
});
Promise.all(promises).then(() => {
console.log('the array with postCounters AND myRate data')
})
答案 1 :(得分:1)
看看这是否有帮助。
let promises = []
querySnapshot.forEach(function (doc) {
promises.push(
promiseFunction()
.then((postCounters) => {
return promiseFunction2();
})
)
})
Promise.all(promises).then((values) => {
console.log(values);
})
答案 2 :(得分:1)
为什么你不使用这样的承诺?!
let promises = [],
promiseArr = [];
querySnapshot.forEach(function (doc) {
promises.push(
promiseFunction()
);
});
Promise.all(promises).then((res) => {
res.forEach(itm => {
promiseArr.push(
promiseFunction2()
);
});
Promise.all(promiseArr).then((res) => {
console.log('save array with postCounters AND myRate data');
});
});
答案 3 :(得分:1)
使用这种方式
let addQueue = q();
for (let i = 0; i < querySnapshot.length; i++){
addQueue = addQueue.then(promisFunction.bind(null, querySnapshot[i]))
.then(function (r) {
console.log('OK: ', r);
})
}
function promisFunction(doc){
let deferred = q.defer();
promisFunction2().then((postCounters) =>{
and function for myRate
deferred.resolve({ a: doc, b: postCounters c: myRate }) }
})
return deferred.promise//
}
addQueue.then(function (result) {
console.log('your result is here');
})
答案 4 :(得分:1)
在您的示例中,您不会显示promiseFunction2
需要等待promiseFunction
的原因。这些功能都没有返回某些内容,因此不清楚您声称缺少什么价值。
如果promiseFunction2
需要在promiseFunction
之后运行,那么您可以执行以下操作:
let promises = [];
querySnapshot.forEach(function (doc) {
promises.push(
promiseFunction()
.then((postCounters) => {
return promiseFunction2()//missing return here
.then((myRate) => {
console.log('save array with postCounters AND myRate data');
return [postCounters,myRate];//missing return here
})
})
);
})
如果您不需要等待promiseFunction
完成promiseFunction2
,那么您可以这样做:
let promises = [];
querySnapshot.forEach(function (doc) {
promises.push(
Promise.all(
promiseFunction(),
promiseFunction2()
)
);
})
在这两种情况下,结果都是一个数组数组:
Promise.all(promises)
.then(
function(results){
results.forEach(
function(result){
console.log("postCounters:",result[0]);
console.log("myRate:",result[1]);
}
)
}
)