4 ms·
I like doing stuff like function ensureFoo() { let promise = Promise.resolve(); if (!foo) { promise = getFoo(); } return prom
by Bahamut 11y ago
I like doing stuff like
function ensureFoo() {
let promise = Promise.resolve();
if (!foo) {
promise = getFoo();
}
return promise;
}
- Guillaume86 11y agoShoudn't you store the initial fooPromise and use it instead? var ensureFooEquivalent = getFoo();
- Bahamut 11y agoSometimes the data you want is already available - no need to make an extra request in those cases.
- hippich 11y agoCheck lodash's memoize method
- untog 11y agoOr don't, as it requires adding an extra library.
- Guillaume86 11y agoLodash provides separate packages: https://www.npmjs.com/package/lodash.memoize https://www.npmjs.com/package/lodash.memoize
- inglor 11y agoDidn't you mean to `Promise.resolve(foo)`? It's also generally safer to cache promises and not values to avoid race conditions (multiple requests).