Skip to content

Instantly share code, notes, and snippets.

@dumboluzz
Created August 9, 2022 03:13
Show Gist options
  • Save dumboluzz/220b4c7ade647d5978e7c627c04a3e4e to your computer and use it in GitHub Desktop.
Save dumboluzz/220b4c7ade647d5978e7c627c04a3e4e to your computer and use it in GitHub Desktop.
// time is the simulated fetch response time in milliseconds
const fakeFetch = (result, time) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(result);
}, time);
});
};
const getRandom = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
await (async () => {
const user = await fakeFetch(
{ id: 69, name: "Tnecniv" },
getRandom(100, 500)
);
const [comments, books, cats, dogs] = await Promise.all([
fakeFetch(`${user.id}-comments`, getRandom(500, 1000)),
fakeFetch(`${user.id}-books`, getRandom(500, 1000)),
fakeFetch(`${user.id}-cats`, getRandom(500, 1000)),
fakeFetch(`${user.id}-dogs`, getRandom(500, 1000)),
]);
console.log(books, comments, cats, dogs);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment