Skip to content

Aggregate header promises #1425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Aggregate header promises #1425

wants to merge 1 commit into from

Conversation

m1heng
Copy link

@m1heng m1heng commented Jan 28, 2023

No description provided.

@ferdikoomen
Copy link
Owner

@m1heng sorry for the late reply, but what is the advantage of using Promise.all() in this case?

@aloisklink
Copy link
Contributor

sorry for the late reply, but what is the advantage of using Promise.all() in this case?

I'm not related to @m1heng, but using Promise.all() runs all of the promises in parallel:

It is typically used when there are multiple related asynchronous tasks that the overall code relies on to work successfully — all of whom we want to fulfill before the code execution continues.

Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all#description

Image that you had to request a TOKEN from an auth server, and you use the ETag HTTP header to cache responses when possible:

import {setTimeout} from "node:timers/promises";
const confg = {
  TOKEN: async() => {
    await setTimeout(1000); // fake a slow connection to auth server
    return "my-constantly-changing-token";
  },
  HEADERS: async() => {
    await setTimeout(1000); // fake a slow connection to cache DB
    return {ETag: "my-hash-of-already-cached-data"};
  },
};

Currently, it will take 2000ms to get the token, then the headers, but if you use Promise.all(), it will only take 1000ms, since they run in parallel.

@ferdikoomen
Copy link
Owner

@m1heng there is a conflict in the PR, but I get your idea, i will merge the changes manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants