如何在节点中序列化异步操作数组?

时间:2020-07-30 14:28:09

标签: javascript asynchronous axios

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

这是我在节点中序列化http get方法的解决方案。 但无论promiseQueue[curIdx]是否已解决Promise,似乎await都行不通,import pymysql pymysql.install_as_MySQLdb() 将放弃执行。 有什么解决方案可以满足我的需求?

1 个答案:

答案 0 :(得分: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;
};

我解决了这个问题。

相关问题