Skip to content

Instantly share code, notes, and snippets.

@kevinvdburgt
Created December 16, 2021 16:00
Show Gist options
  • Save kevinvdburgt/368f844650dff2b62559f54666971b20 to your computer and use it in GitHub Desktop.
Save kevinvdburgt/368f844650dff2b62559f54666971b20 to your computer and use it in GitHub Desktop.
const simulateSlowHttpRequest = async (dummyData: string): Promise<string> =>
new Promise<string>((resolve) => setTimeout(() => resolve(dummyData), 2000));
const bootstrap = async (): Promise<void> => {
/**
* Test A
*/
const startA = Date.now();
const aA = await simulateSlowHttpRequest('a');
const aB = await simulateSlowHttpRequest('b');
const aC = await simulateSlowHttpRequest('c');
console.log(`Test A took ${Date.now() - startA}ms`, { aA, aB, aC });
// ^ Test A took 6009ms { aA: 'a', aB: 'b', aC: 'c' }
/**
* Test B
*/
const startB = Date.now();
const [bA, bB, bC] = await Promise.all([
simulateSlowHttpRequest('d'),
simulateSlowHttpRequest('e'),
simulateSlowHttpRequest('f'),
]);
console.log(`Test B took ${Date.now() - startB}ms`, { bA, bB, bC });
// ^ Test B took 2003ms { bA: 'd', bB: 'e', bC: 'f' }
};
bootstrap().catch((error: unknown) => {
console.error(error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment