EExcel 丞燕快速查詢2

EExcel 丞燕快速查詢2
EExcel 丞燕快速查詢2 https://sandk.ffbizs.com/
顯示具有 promise 標籤的文章。 顯示所有文章
顯示具有 promise 標籤的文章。 顯示所有文章

Promise.all map



const arr = {};

await Promise.all(
  UsersQuery.map(async function (data) {
    const city = await db.sequelize.query(`
      select * from city
    `, 
      type: db.sequelize.QueryTypes.SELECT 
    });

    arr[data.user_id] = city[0].name;

  })
}


UsersQuery.forEach(async function (data, index) {
  this[index].name = arr[data.user_id];
}, UsersQuery);

promise.all error catch

https://stackoverflow.com/questions/30362733/handling-errors-in-promise-all



Promise.all(state.routes.map(function(route) {
  return route.handler.promiseHandler().catch(function(err) {
    return err;
  });
}))
.then(function(arrayOfValuesOrErrors) {
  // handling of my array containing values and/or errors. 
})
.catch(function(err) {
  console.log(err.message); // some coding error in handling happened
});


Alternately, if you have a case where you don't particularly care about the values of the resolved promises when there is one failure but you still want them to have run, you could do something like this which will resolve with the promises as normal when they all succeed and reject with the failed promises when any of them fail:



function promiseNoReallyAll (promises) {
  return new Promise(
    async (resolve, reject) => {
      const failedPromises = []

      const successfulPromises = await Promise.all(
        promises.map(
          promise => promise.catch(error => {
            failedPromises.push(error)
          })
        )
      )

      if (failedPromises.length) {
        reject(failedPromises)
      } else {
        resolve(successfulPromises)
      }
    }
  )
}

async await Promise.all

async getaaa()
async getbbb()
async getccc()


=======new way=======
async  refreshall(){

  await Promise.all( xxxxxx.map(async (value, index) =>{
    aaa = await getaaa()
  }))

  await Promise.all( xxxxxx.map(async (value, index) =>{
     bbb = await getbbb()

  }))


  await Promise.all( xxxxxx.map(async (value, index) =>{
    ccc = await getccc()
  }))

}

=======old way=======
async  refreshall(){

  await Promise.all( xxxxxx.map(async (value, index) =>{
    aaa = await getaaa()
    bbb = await getbbb()
    ccc = await getccc()

  }))

}


1. map usually return something, here just throw
2. if use foreach(), get error.  Like  foreach is not function, can't async await.

old way  aaa order is good, but bbb ccc order is bad.
new way  all aaa bbb ccc order is ok.