Javascript 如何确保一批请求已准备就绪

Javascript 如何确保一批请求已准备就绪,javascript,ajax,for-loop,callback,Javascript,Ajax,For Loop,Callback,我需要通过API获取接下来12个月内每个日期的价格 一个要求是请求不能超过2个月。所以我需要把它分成6个请求 因此,我从当前日期到+1个月,再迭代5次,得到剩余的10个月,如下所示: const getPeriod = (dateIn, dateOut) => { axios .post( Utils.apiUrl, { hotelCodes: Utils.hotelId.split(','), dateIn: dateI

我需要通过API获取接下来12个月内每个日期的价格

一个要求是请求不能超过2个月。所以我需要把它分成6个请求

因此,我从当前日期到+1个月,再迭代5次,得到剩余的10个月,如下所示:

const getPeriod = (dateIn, dateOut) => {
  axios
    .post(
      Utils.apiUrl,
      {
        hotelCodes: Utils.hotelId.split(','),
        dateIn: dateIn, 
        dateOut: dateOut, 
      }
    )
    .then((response) => {
      const calendarPrices = response.data.CalendarPrices
      var now = new Date()
      for (let d = 0; d < calendarPrices.length; d++) {
        const date = new Date(calendarPrices[d].Date)
        date.setHours(12, now.getMinutes(), now.getSeconds()) 
        calendarPrices[d].dateObj = date
      }
      if (Utils.calendarPrices) {
        Utils.calendarPrices = Utils.calendarPrices.concat(calendarPrices)
      } else {
        Utils.calendarPrices = calendarPrices
      }
    })
    .catch((err) => {
      console.log(err)
    })
}

const getNextMonths = () => {
  for (let r = 1; r <= requestsToMake; r++) {
    let dateIn = new Date()
    dateIn.setMonth(dateIn.getMonth() + 2 * r)
    const monthIn =
      (dateIn.getMonth() < 9 ? '0' : '') +
      (parseInt(dateIn.getMonth(), 10) + 1)

    let dateOut = new Date()
    dateOut.setMonth(dateOut.getMonth() + 1 + 2 * r)
    const monthOut =
      (dateOut.getMonth() < 9 ? '0' : '') +
      (parseInt(dateOut.getMonth(), 10) + 1)
    const lastDayOfMonth = new Date(
      dateOut.getFullYear(),
      dateOut.getMonth() + 1,
      0
    )

    getPeriod(
      `${dateIn.getFullYear()}-${monthIn}-01`,
      `${dateOut.getFullYear()}-${monthOut}-${lastDayOfMonth.getDate()}`
    )

  }
}



// get current month and the next one  
  axios
  .post(
    Utils.apiUrl,
    {
      hotelCodes: Utils.hotelId.split(','),
      dateIn: `${dateIn.getFullYear()}-${
        (dateIn.getMonth() + 1 < 10 ? '0' : '') +
        parseInt(dateIn.getMonth() + 1, 10)
      }-${(dateIn.getDate() < 10 ? '0' : '') + dateIn.getDate()}`,
      dateOut: `${dateOut.getFullYear()}-${
        (dateOut.getMonth() + 1 < 10 ? '0' : '') +
        parseInt(dateOut.getMonth() + 1, 10)
      }-${
        (lastDayOfMonth.getDate() < 10 ? '0' : '') +
        lastDayOfMonth.getDate()
      }`,
    }
  )
  .then((response) => {
    const calendarPrices = response.data.CalendarPrices
    var now = new Date()
    for (let d = 0; d < calendarPrices.length; d++) {
      const date = new Date(calendarPrices[d].Date)
      date.setHours(12, now.getMinutes(), now.getSeconds()) 
      calendarPrices[d].dateObj = date
    }

    if (Utils.calendarPrices) {
      Utils.calendarPrices = Utils.calendarPrices.concat(
        calendarPrices
      )
    } else {
      Utils.calendarPrices = calendarPrices
    }

   
    datepicker = new HotelDatepicker(input, options)
    // get 10 more months
    getNextMonths()
  })
const getPeriod=(dateIn,dateOut)=>{
axios
.邮政(
Utils.apirl,
{
hotelCodes:Utils.hotelId.split(','),
dateIn:dateIn,
dateOut:dateOut,
}
)
。然后((响应)=>{
const calendarPrices=response.data.calendarPrices
var now=新日期()
for(设d=0;d{
console.log(错误)
})
}
const getNextMonths=()=>{
对于(设r=1;r{
const calendarPrices=response.data.calendarPrices
var now=新日期()
for(设d=0;d
我遇到的问题是,当日历准备就绪时,只有第一个请求完成时,这些调用才是异步调用

在我初始化日历之前,如何确保已收到所有日期


我正在考虑创建一个递归函数,但我不知道如何处理回调。

我做了类似的事情,需要批量处理请求。解决方案是使用promisse.all()。我创建了一系列承诺,然后使用promise.all一起解决所有问题,只有在所有承诺都得到解决时才能得到响应。也许这可以帮助您解决问题。谢谢提醒,我看到axios的承诺存在。所有。我会看看我是否能让它起作用。^^我做了类似的事情,需要执行批量请求。The解决方案是使用promisse.all()。我创建了一系列承诺,然后使用promise.all一起解决所有问题,只有在所有承诺都得到解决时才能得到响应。也许这可以帮助您。谢谢提醒,我看到axios的承诺是存在的。全部。我会看看是否能让它生效^^