Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Return consistent promise object for repeated calls with the same key#3

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
keithamus merged 14 commits intogithub:masterfromakenneth:return-wrapped-promise
Jan 18, 2021

Conversation

akenneth
Copy link
Contributor

Overview

While using memoize in our code we identified an issue where the order of promise execution got scrambled by the use of memoize.
This PR fixes this issue.

Root cause

The first call to memoize with a certain key returns one promise, all subsequent calls to memoize with the same key return a different promise.
This is because the first call creates a new promise with the.catch call and returns it, but caches the original "inner" promise.
Following calls to memoize return only the "inner" promise.

Suggested fix

In the case of having a promise object as the result of the evaluated function, cache the complex promise with thecatch call in it.
This way the first and subsequent calls will return the same promise and all will register their events on it.

Details:

We are using memoize to memoize the suggested autocomplete entries for a text input in our web page, they all have the same cache key.
When the user is typing a string into the input, each letter invokes theonchange event that uses memoize and awaits the promise that fetches data from the server.

What we see is that when the promise returned by the memoized function is resolved, the first caller'sthen method is being invoked last (instead of first as expected).

Repro steps:

  1. Call memoize with a promise returning function with key 'a' and await it.
  2. While it's executing have one (or more) calls to the same key and await it.

Expected behavior - thethen statement from item1 is invoked before thethen statement of item2.

Note: also see the unit test in this PR to illustrate this scenario. This test is failing in the currentmaster branch.

anleac reacted with thumbs up emoji
@akennethakenneth requested a review froma team as acode ownerJanuary 18, 2021 10:40
@akenneth
Copy link
ContributorAuthor

Thanks@anleac for helping out here!

anleac reacted with heart emoji

Copy link

@octatoneoctatone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

👍 LGTM

@akenneth
Copy link
ContributorAuthor

@keithamus should I bump the package version as well?

Copy link
Contributor

@koddssonkoddsson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This looks good to me. I did a bit of red-green testing to make sure that the test fails when with the original code inmaster and it checks out on my machine.

I'll leave the final approval to@keithamus

akenneth reacted with heart emoji
@koddsson
Copy link
Contributor

@keithamus should I bump the package version as well?

Normally we do that as part of the publishing process after merging the PR.

akenneth reacted with thumbs up emoji

This test can be run in isolation and more readily describes theexpected pattern of behavior. This simplifies debugging later.
The caught Promise _must_ be allowed to be re-caught to allow for properrejection handling. This test affirms that.
The earlier test which affirms Promise equality with subsequent callsguarantees the behavior of sequential ordering as per the spec. It ispointless to test this as if the test breaks it is a bug with the JSengine and one we're unlikely able to fix (or is at least out of thescope of this project). Our interest is only in the earlier test ofreferential equality.This test also tests the cache implementation, but this is needless asthere are other tests which affirm these semantics.
@keithamus
Copy link
Contributor

I've simplified the test cases to be more explicit about the behaviours we expect:

  1. That memoized calls return the same Promise each call.
  2. That the memoized Promise can becaught without leaking errors.

I think this is good to go now 😉 😄

@keithamuskeithamus merged commit260db15 intogithub:masterJan 18, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
2 more reviewers

@koddssonkoddssonkoddsson approved these changes

@octatoneoctatoneoctatone approved these changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@akenneth@koddsson@keithamus@octatone

[8]ページ先頭

©2009-2025 Movatter.jp