-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Fix consecutive non awaited calls #25
Fix consecutive non awaited calls #25
Conversation
index.js
Outdated
let resultError; | ||
try { | ||
return await Promise.resolve(result); | ||
return await result; | ||
} catch (error) { | ||
resultError = error; | ||
throw error; | ||
} finally { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sindresorhus Since we're capturing the error, I think we should use Promise.allSettled
instead of try/catch/finally. This feels cleaner:
const [{reason}] = await Promise.allSettled([promise])
if (reason && !cachePromiseRejection) {
cache.delete(key);
} else if (maxAge) {
// Promise fulfilled, so start the timer
cache.set(key, {
data: promise,
maxAge: Date.now() + maxAge
});
}
return promise;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't used allSettled
before but it looks super clean in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once added, p-memoize will also need to bump to a minimum of Node 12 and therefore ESM https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can handle the Node.js bumping and ESM stuff after this PR.
Thanks for fixing, @krystofwoldrich 👍 |
Thanks for the prompt comments and reviews. @sindresorhus and @fregante |
Based on our discussion after merging PR #24 I'm opening a new one hopefully solving mentioned issues.