admin管理员组文章数量:1397093
I have a situation where I need to loop through an array of URLs and fetch the result, but I need to preserve the order of requests, that is the first request should be "saved" (write to a file) first, and so on. The result from the requests are text files and their content do not have any data that reveals the original URL or order.
I have made a demo below, which fetches dummy JSON data.
let promises = [];
let data = [];
let i = 1;
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
const fn = async() => {
while (i < 5) {
promises.push(
fetch('/' + i, {
mode: 'cors',
headers: headers
})
.then(response => response.json())
.then(json => {
data.push(json)
})
)
i++;
}
await Promise.all(promises).then(() => {
console.log(data);
})
}
fn();
I have a situation where I need to loop through an array of URLs and fetch the result, but I need to preserve the order of requests, that is the first request should be "saved" (write to a file) first, and so on. The result from the requests are text files and their content do not have any data that reveals the original URL or order.
I have made a demo below, which fetches dummy JSON data.
let promises = [];
let data = [];
let i = 1;
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
const fn = async() => {
while (i < 5) {
promises.push(
fetch('https://reqres.in/api/users/' + i, {
mode: 'cors',
headers: headers
})
.then(response => response.json())
.then(json => {
data.push(json)
})
)
i++;
}
await Promise.all(promises).then(() => {
console.log(data);
})
}
fn();
If you test the above code, you can see that the results are in random order (based on id), and since the files in my original code are text files, I can't sort it after fetch.
Share Improve this question edited Mar 12, 2020 at 16:23 ADyson 62.2k16 gold badges78 silver badges92 bronze badges asked Mar 12, 2020 at 16:21 AkshayAkshay 14.4k5 gold badges48 silver badges72 bronze badges 2-
1
You should push the results to
data
in thePromise.all()
code. – Barmar Commented Mar 12, 2020 at 16:23 - Thanks @Barmar got it. – Akshay Commented Mar 12, 2020 at 16:26
2 Answers
Reset to default 7Use the values the promises resolve to. No need to keep a separate data
array and manually add the values to it. Promise.all
makes sure the values are in the same order as the promises.
let promises = [];
let i = 1;
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
const fn = async() => {
while (i < 5) {
promises.push(
fetch('https://reqres.in/api/users/' + i, {
mode: 'cors',
headers: headers
})
.then(response => response.json())
)
i++;
}
await Promise.all(promises).then(data => {
console.log(data);
})
}
fn();
And since you are in an async function you can just do:
var data = await Promise.all(promises);
console.log(data);
Also, you can call this way by creating an array of urls and then calling Promise.all
which maps through those urls and returns a single promise with all responses in an array:
let headers = new Headers({'Content-Type':'application/json','Accept':'application/json'});
let params = { mode: 'cors', headers};
(async function() {
// Create an array or urls
const urls = [...Array(4).keys()].map(i => `https://reqres.in/api/users/${i+1}`);
const response = await Promise.all(urls.map(url => fetch(url, params)))
const data = await Promise.all(response.map(res => res.json()))
console.log(data)
}());
本文标签: javascriptFetch in loopkeep result orderStack Overflow
版权声明:本文标题:javascript - Fetch in loop, keep result order - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744148678a2592960.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论