从 setTimeout 调用的递归收益

Recursive yield called from setTimeout

我正在使用 API 和 rate-limit,每次达到我的速率限制时它 returns header retry-after 指定秒数等待速率限制重置。

我需要:

目前我的解决方案:

async *indicators(items: string[]): AsyncIterableIterator<any[]> {
  const res = await Promise.allSettled(items.map((item) => this.makeRequest(item)))

  const fulfilledRequests = res.filter((r) => r.status === 'fulfilled') as PromiseFulfilledResult<any>[]

  for (const { value } of fulfilledRequests) {
    console.log('Yielding')
    yield value
    console.log('Yielded')
  }

  const rejectedRequest = res.find((r) => r.status === 'rejected') as any
  const failedItems = res.filter((p) => p.status === 'rejected').map(({ reason }: any) => reason.item)

  if (failedItems.length === 0 || !failedItems?.reason?.retryAfter)
    return Logger.log(`No more items to check`)

  setTimeout(this.indicators(failedItems).next.bind(this), rejectedRequest.reason.retryAfter)
}

async makeRequest(item: string): Promise<Indicator[]> {
  try {
    const { data: { data } } = await firstValueFrom(this.httpService.post('https://api.io', { item }))
    return data
  } catch (error) {
    throw { retryAfter: error.response.headers['retry-after'] * 1000, symbol }
  }
}

main() {

  for await (const item of this.indicators(['', ''])) {
    console.log(item)
  }

}

我在 Node v16 上使用带有 Typescript 的 NestJS。

我建议独立重试每个请求,直到成功或放弃。

下面的代码未测试


async function main() {
  const promises = ["", ""].map(item=>makeRequestUntilDone(item));
  const results = await Promise.allSettled(promises);
  for(const r of results) {
      if(r.status === 'fulfilled') {
        // process result
        console.log(r.value);
      } else {
        // process error if you sometimes
        // throw in `makeRequestUntilDone`
        console.log(r.reason);
      }
  }
}

async function makeRequestUntilDone(item: string) {
  while(true){
    try {
      const { data: { data } } = await firstValueFrom(this.httpService.post('https://api.io', { item }))
      return data
    } catch (error) {
      // `throw error` if you don't wanna retry 
      // anymore or it is not a retryable error,
      // otherwise delay and continue
      const retryAfter = error.response.headers['retry-after'] * 1000;
      await delay(retryAfter);
    }
  }
}

function delay(ms: number) {
  return new Promise(function(resolve) {
    setTimeout(resolve, ms);
  });
}

那你可以

我通过使用 do while 循环等待一段时间后重试使其工作。

async *listMeetings(meetingIds: string[]): AsyncIterableIterator<MeetingResponse> {
  let hasMore = false
  do {
    const res = await Promise.allSettled(meetingIds.map((id) => this.makeRequest(id)))
    const fulfilledRequests = res.filter((r) => r.status === 'fulfilled')
    for (const { value } of fulfilledRequests) {
      yield value
    }
    const rejectedRequest = res.find((r) => r.status === 'rejected')
    const failedMeetings = res.filter((p) => p.status === 'rejected').map(({ reason }: any) => reason.meetingId)
    if (failedMeetings.length === 0 || !rejectedRequest?.reason?.retryAfter) {
      hasMore = false
    } else {
      await new Promise<void>((resolve) => setTimeout(() => resolve(), rejectedRequest.reason.retryAfter))
      yield* this.listMeetings(failedMeetings)
    }
  } while (hasMore)
}

main() {
  for await (const meeting of this.client.listMeetings([])) {
    console.log(meeting)
  }
}