【问题标题】:how can I serialize an array of async operation in node?如何序列化节点中的异步操作数组?
【发布时间】:2020-07-31 02:22:05
【问题描述】:
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;
};

这是我在 node.js 中序列化 http get 方法的解决方案。 但是好像不行,不管promiseQueue[curIdx]是不是已经解析的Promiseawait都会放弃它的执行。 有什么解决方案可以满足我的需要吗?

【问题讨论】:

  • 看看this
  • 什么是 promiseQueue[curIdx]?数组为空
  • 谢谢大家,这个问题我已经解决了,我发现queue impl不能序列化ops。

标签: javascript asynchronous axios


【解决方案1】:
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;
};

我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2023-03-27
    • 2016-12-26
    • 2013-07-29
    • 2019-08-25
    • 2023-03-16
    • 1970-01-01
    • 2019-01-16
    相关资源
    最近更新 更多