JavaScript Wait for 1 promise before all other promises wait for promise before continuing,wait for promise before return,

Apologize in advance about the ambiguous title.

To better explain the question - I have two functions in a fetch-form-data.js that look something like...

let cachedBaseUrl;

const getBaseUrl = () => {
  if (cachedBaseUrl) return Promise.resolve(cachedBaseUrl)

  return fetch('https://some-baseurl-endpoint.com')
    .then(baseUrl => {
      cachedBaseUrl = baseUrl
      return baseUrl
    });
}

const getFormData = formId =>
  getBaseUrl()
    .then(baseUrl => fetch('https://form-data-endpoint.com'))

module.exports = {
  getFormData
}

getFormData is exported and called hundreds of times in a loop elsewhere. The problem is getFormData depends on getBaseUrl, so calling getFormData hundreds of times also calls getBaseUrl hundreds of times, which frequently errors because I'm guessing the baseUrl endpoint doesn't like getting hit so rapidly.

I would like to instead ensure that getBaseUrl only gets hit once, and have all subsequent getFormData calls "wait" on getBaseUrl resolving before moving on.

One solution would be to export both getBaseUrl and getFormData wherever getFormData is being used, await getBaseUrl, then call getFormData however many times as needed. Something like...

async () => {
  const baseUrl = await getBaseUrl();
  const formData = [];

  for (let form of forms) {
    formData.push(getFormData(form));
  }

  await Promise.all(formData);
  ...
}

However, the usage of getFormData is spread across the codebase. It would be nice to simply abstract away getBaseUrl as is the current implementation and only expose getFormData, but solve the issue of "awaiting" all subsequent getFormData calls until at least the first getBaseUrl call is resolved.

My feeble attempt is to cache baseUrl, as seen in the above codeblock, but that doesn't "stop" immediate subsequent getBaseUrl calls from occurring, which calls another fetch because nothing is cached yet.

How would I go about implementing something like this?

Answer:1

Your caching solution is a good start, by caching the pending promise as well I think it solves the problem elegantly.

let cachedBaseUrl;

const getBaseUrl = () => {
  if (cachedBaseUrl) return Promise.resolve(cachedBaseUrl)
  return cachedBaseUrl = fetch('https://some-baseurl-endpoint.com');
}

const getFormData = formId =>
  getBaseUrl()
    .then(baseUrl => fetch('https://form-data-endpoint.com'))

module.exports = {
  getFormData
}
Answer:2

I have a search button which displays autocomplete suggestions as the user types as demonstrated here: https://jsfiddle.net/z8qpw3yL/ The issue is that on clicking the suggested text, the search bar ...

I have a search button which displays autocomplete suggestions as the user types as demonstrated here: https://jsfiddle.net/z8qpw3yL/ The issue is that on clicking the suggested text, the search bar ...

  1. prevent search from start menu
  2. prevent search engines from indexing
  3. prevent search engines from indexing a page
  4. prevent search engines from indexing wordpress
  5. chrome prevent search from address bar
  6. firefox prevent search from address bar
  7. prevent search engine from indexing site
  8. prevent search engines from indexing website
  9. prevent search bar from disappearing

I have uploaded one document on scribd I have included links within the document for ease of navigation. It works fine when opened in Chrome, but not working well in Firefox 3.6. It displays the ...

I have uploaded one document on scribd I have included links within the document for ease of navigation. It works fine when opened in Chrome, but not working well in Firefox 3.6. It displays the ...

  1. window location hash change
  2. window location hash replace
  3. window location hash change event
  4. window location hash javascript
  5. window location hash not working
  6. window.location.hash
  7. window.location.hash jquery
  8. window.location.hash xss
  9. window.location.hash.substr(1)
  10. window.location.hash remove #
  11. window.location.hash without #
  12. window.location.hash js
  13. window.location.hash.substr
  14. window.location.hash.indexof
  15. window.location.hash dom xss
  16. window.location.hash mdn
  17. window.location.hash example
  18. window.location.hash without scrolling
  19. window.location.hash prevent scroll
  20. window.location.hash empty

I am trying to figure out how to list 3 check-boxes at the top of a page that, when checked, auto reveal more info on the page. Using php if I can hide the info, but how will it update page? I am ...

I am trying to figure out how to list 3 check-boxes at the top of a page that, when checked, auto reveal more info on the page. Using php if I can hide the info, but how will it update page? I am ...

I have 4 icons on which i'm trying for active link like this - but it's not working don't know why i guess i'm missing some steps also when i'm changing router i need to highlight that router tab. ...

I have 4 icons on which i'm trying for active link like this - but it's not working don't know why i guess i'm missing some steps also when i'm changing router i need to highlight that router tab. ...

  1. angular active link color
  2. angular active link css
  3. angular active link
  4. angular active link class
  5. angular active link router
  6. angular active link directive
  7. angular material active link
  8. angular highlight active link
  9. angular active router link
  10. angular navigation active link
  11. angular active nav link
  12. angular bootstrap active link
  13. angular 6 active link
  14. angular menu active link
  15. angular set active link
  16. angular 7 active link
  17. angular get active link
  18. angular active router link class
  19. angular disable active link
  20. angular 4 active link