Skip to content

Instantly share code, notes, and snippets.

@ZowieTao
Created November 3, 2023 00:44
Show Gist options
  • Save ZowieTao/fedb664a80ff93d08ed60439858261d0 to your computer and use it in GitHub Desktop.
Save ZowieTao/fedb664a80ff93d08ed60439858261d0 to your computer and use it in GitHub Desktop.
The `concurRequest` function allows concurrent HTTP requests to multiple URLs, returning the results in the original order. It improves efficiency by fetching data from multiple endpoints simultaneously.
/**
* concur request and return result sortby.
*
* @param {string[]} urlList - The url which be will requested.
* @param {number} capacity - The max number of concurRequest.
* @returns {Promise<any[]>}.
*/
function concurRequest(urlList, capacity) {
return new Promise((resolve) => {
if (urlList.length === 0) {
resolve([]);
return;
}
const results = [];
let index = 0; // next request
let count = 0; // current request finished count
// send request
async function request() {
if (index === urlList.length) {
return;
}
const currentIdx = index;
const url = urlList[index];
index++;
console.log(`url: ${url} start fetch`);
try {
const resp = await _fetch(url);
// resp add into results
results[currentIdx] = resp;
} catch (error) {
// err add into results
results[currentIdx] = `err:${error}`;
} finally {
// condition about
count++;
if (count === urlList.length) {
console.log("finish");
resolve(results);
}
request();
}
}
const times = Math.min(capacity, urlList.length);
for (let index = 0; index < times; index++) {
request();
}
});
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function _fetch(str) {
return new Promise((resolve, reject) => {
const needResolve = Math.floor(Math.random() * 100) % 2 === 0;
const result = str.repeat(2);
setTimeout(() => {
if (needResolve) {
resolve(result);
} else {
reject(str);
}
}, getRandomNumber(1000, 3000));
});
}
concurRequest(["1", "2", "3", "4", "5", "6", "7", "8"], 3).then((data) => {
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment