Skip to content
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

Merged
merged 7 commits into from
Sep 19, 2021

Conversation

krystofwoldrich
Copy link
Contributor

Based on our discussion after merging PR #24 I'm opening a new one hopefully solving mentioned issues.

index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
index.js Outdated
Comment on lines 31 to 37
let resultError;
try {
return await Promise.resolve(result);
return await result;
} catch (error) {
resultError = error;
throw error;
} finally {
Copy link
Collaborator

@fregante fregante Sep 18, 2021

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;

Copy link
Contributor Author

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.

Copy link
Collaborator

@fregante fregante Sep 18, 2021

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

Copy link
Owner

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.

@sindresorhus sindresorhus merged commit 343ac6e into sindresorhus:main Sep 19, 2021
@sindresorhus
Copy link
Owner

Thanks for fixing, @krystofwoldrich 👍

@krystofwoldrich
Copy link
Contributor Author

Thanks for the prompt comments and reviews. @sindresorhus and @fregante

niksy pushed a commit to niksy/p-memoize that referenced this pull request Nov 10, 2021
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.

None yet

3 participants