1.串行执行
多个接口串行执行
const requestArry = [() => api.request1(), () => api.request2(), () => api.request3()];
const finallyPromise = requestArry.reduce(
(currentPromise, nextRequest) => currentPromise.then(() => nextRequest()),
Promise.resolve()
);
2.串行执行
相同请求未答应,二次或多次发出
const promises = {};
function request(type,url,data) {
const key = JSON.stringify([type, url, data]);
// 通过key判断是否在请求
if (promises[key]) {
return promises[key];
}
const fetchPromise = fetch(url, {
method: type,
data: JSON.stringify(data)
})
.then(response => response.json())
.finally(() => {
delete promises[key];
});
return promises[key] = fetchPromise;
}