工作的时候要有承诺,里面有承诺。

时间:2022-03-19 20:16:40

Im doing the following code:

我正在做以下代码:

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')
})

Things are getting wrong. The returned Promise.all doesn't wait until promiseFunction2 is run, so the second function is not working and im not getting the promiseFunction2 data inside my array, just the first.

错误的东西。返回的承诺。所有这些都不会等到promiseFunction2运行之后,所以第二个函数不会工作,我也不会在我的数组中获得promiseFunction2数据,只有第一个。

What i think is happening is: Promise.all is getting after all promiseFunction is run, BUT it doesn't wait until the second function, i would like to wait BOTH to complete before getting promise all, should i use two Promise.all???

我认为正在发生的是:承诺。所有的都得到了promiseFunction运行后,但它并不是等到第二个函数,我想两个都完成才能得到承诺全部,我应该使用两个承诺全部吗?

Im not very familiar with the syntax of Promise.all and im just using the promiseFunction().then() approach

我对承诺的句法不太熟悉。我只使用promiseFunction()方法

(event to get my code consistent to scale well)

(让我的代码保持一致的事件)

5 个解决方案

#1


1  

Why you don't use promise like this?!

你为什么不用这样的承诺?!

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');
    });

});

#2


2  

You should use map to turn one array into another. The problem is that you're not returning the promise chain generated from promiseFunction2:

您应该使用映射将一个数组转换为另一个数组。问题是你没有返回来自promiseFunction2的承诺链:

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')
});

Or, for far less indentation noise, use 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')
})

#3


1  

Use this way

使用这种方式

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  

In your example you don't show why promiseFunction2 needs to wait for promiseFunction. Neither of the functions are returning something so it's not clear what value you claim is missing.

在您的示例中,您没有说明为什么promiseFunction2需要等待promiseFunction。这两个函数都没有返回某个值,因此不清楚您声明的值是否丢失。

If promiseFunction2 needs to run after promiseFunction then you can do:

如果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
      })
    })
  );
})

If you don't need to wait for promiseFunction to finish to start promiseFunction2 then you can do:

如果您不需要等待promiseFunction完成后才启动promiseFunction2,那么您可以这样做:

let promises = [];
querySnapshot.forEach(function (doc) {
  promises.push(
    Promise.all(
      promiseFunction(),
      promiseFunction2()
    )
  );
})

In both cases the result is an array of array:

在这两种情况下,结果都是数组:

Promise.all(promises)
.then(
  function(results){
    results.forEach(
      function(result){
        console.log("postCounters:",result[0]);
        console.log("myRate:",result[1]);
      }
    )
  }
)

#5


0  

See if this helps.

看看这有助于。

  let promises = []
  querySnapshot.forEach(function (doc) {
    promises.push(
      promiseFunction()
        .then((postCounters) => {
          return promiseFunction2();
        })
    )
  })
  Promise.all(promises).then((values) => {
    console.log(values);
  })

#1


1  

Why you don't use promise like this?!

你为什么不用这样的承诺?!

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');
    });

});

#2


2  

You should use map to turn one array into another. The problem is that you're not returning the promise chain generated from promiseFunction2:

您应该使用映射将一个数组转换为另一个数组。问题是你没有返回来自promiseFunction2的承诺链:

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')
});

Or, for far less indentation noise, use 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')
})

#3


1  

Use this way

使用这种方式

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  

In your example you don't show why promiseFunction2 needs to wait for promiseFunction. Neither of the functions are returning something so it's not clear what value you claim is missing.

在您的示例中,您没有说明为什么promiseFunction2需要等待promiseFunction。这两个函数都没有返回某个值,因此不清楚您声明的值是否丢失。

If promiseFunction2 needs to run after promiseFunction then you can do:

如果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
      })
    })
  );
})

If you don't need to wait for promiseFunction to finish to start promiseFunction2 then you can do:

如果您不需要等待promiseFunction完成后才启动promiseFunction2,那么您可以这样做:

let promises = [];
querySnapshot.forEach(function (doc) {
  promises.push(
    Promise.all(
      promiseFunction(),
      promiseFunction2()
    )
  );
})

In both cases the result is an array of array:

在这两种情况下,结果都是数组:

Promise.all(promises)
.then(
  function(results){
    results.forEach(
      function(result){
        console.log("postCounters:",result[0]);
        console.log("myRate:",result[1]);
      }
    )
  }
)

#5


0  

See if this helps.

看看这有助于。

  let promises = []
  querySnapshot.forEach(function (doc) {
    promises.push(
      promiseFunction()
        .then((postCounters) => {
          return promiseFunction2();
        })
    )
  })
  Promise.all(promises).then((values) => {
    console.log(values);
  })