0
const oldGet = Axios.get;
const promiseQueue: Promise<any>[] = [];
const serialGet = async (url: string) => {
  const curIdx = promiseQueue.length - 1;
  if (curIdx != -1) {
    await promiseQueue[curIdx];
  }
  const getData = oldGet(url);
  promiseQueue.push(getData);
  return await getData;
};

This is my solution for serializing http get method in node. But it seems do not work, no matter promiseQueue[curIdx] is a resolved Promise or not, await will give up its execution. It's there any solution meet my need?

BeanBro
  • 51
  • 1
  • 6

1 Answers1

0
const oldGet = Axios.get;
let PrevPromise: Promise<any> | undefined = undefined;
const serialGet = async (url: string): Promise<any> => {
  const innerGet = async () => {
    if (PrevPromise) {
      await PrevPromise;
    }
    console.log(`get ${url}`);
    return await oldGet(url);
  };
  PrevPromise = innerGet();
  return await PrevPromise;
};

I solve this problem.

BeanBro
  • 51
  • 1
  • 6