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()
将放弃执行。
有什么解决方案可以满足我的需求?
答案 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;
};
我解决了这个问题。