gpt4 book ai didi

javascript - Axios 中数组 Promise 的链接

转载 作者:行者123 更新时间:2023-11-27 22:41:11 26 4
gpt4 key购买 nike

我有一系列请求,需要按顺序使用 Axios 发出。

let {files} = this.state, requestQueue = [];
files.forEach(file => requestQueue.push(makeRequest(file.name)));
requestQueue.reduce((curr, next) => {
return curr.then(next);
}, Promise.resolve()).then((res) => console.log(res));

函数makeRequest如下

import Axios from 'axios';

let axiosCustom = Axios.create({
baseUrl: 'localhost:8080',
headers: {
Accept: 'application/json'
}
});

const makeRequest = (title) => {
return axiosCustom({
url: '/api',
method: 'PUT',
params: {
title
}
});
};

该响应只是第一个已解决的响应。我该如何解决这个问题?

最佳答案

这就是使用数组同步链接 axios 的方式。

const axios = require('axios');
function makeRequestsFromArray(arr) {
let index = 0;
function request() {
return axios.get('http://localhost:3000/api/' + index).then(() => {
index++;
if (index >= arr.length) {
return 'done'
}
return request();
});

}
return request();
}

makeRequestsFromArray([0, 1, 2]);

关于javascript - Axios 中数组 Promise 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38737395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com