-
Notifications
You must be signed in to change notification settings - Fork 123
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 delay to return delayed function result for use #1538
Conversation
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.
Nice work and it looks good 👍 Just one question below.
@@ -24,7 +24,7 @@ module.exports = function delay(func, wait) { | |||
|
|||
if (waitingPromise === null) { | |||
waitingPromise = Promise.all([Promise.delay(wait), runningPromise]) |
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.
One question: Will this promise ever be rejected? If it does will this cause a change in behaviour (callback in .then
will not run in this case but in .finally
will always run regardless)?
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 don't think there is a case for that promise to be rejected. My observation is that waitingPromise
is initially a Promise.all
of a Promise.delay
and a Promise.resolve
, and based on the callback, subsequent waitingPromise
will be either:
- Similar to the original (
Promise.all
ofPromise.delay
andPromise.resolve
; or - A
Promise.all
ofPromise.delay
and the previouswaitingPromise
waitingPromise
essentially just boils down to the behaviour of Promise.delay
and Promise.resolve
. Both have no chance of rejection: Promise.delay
without second argument returns undefined on resolve, the same with Promise.resolve
without argument. Thus, waitingPromise
will never be rejected.
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.
Alright :) Thanks for the clarification and detailed explanation 👍
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.
LGTM :)
What is the purpose of this pull request?
Resolves #1537.
Overview of changes:
The part of the
delay
function that caused the issue is specifically this:markbind/packages/core/src/utils/delay.js
Lines 25 to 34 in 5c826ce
Notice the use of
finally
to start calling the delayed function.finally
is a bit finicky, through some experiments, turns out the return offinally
cannot be used in the nextthen
/afterawait
(in fact,finally
does not disrupt the return value of the promise, ref: here) so thereturn funcPromise
is effectively unable to be used.Changing
finally
tothen
fixes the issue, and I believe still preserves the behaviour of the previous implementation.Reproduction and fixed demos is available here: https://codesandbox.io/s/cool-sid-25hmb. Check the console tab on the bottom right part of the page and refresh the "browser" on the right panel to test it over and over. Can play with
flowOriginal
andflowFixed
for the original and fixed implementation, the argument is the number of calls of the delayeddoWork
function (can try1
first to understand the behaviours, I put2
to test consistency of the approach).Notice the condition of the image in the issue is reproduced when using
flowOriginal
and all thedoWork
calls get resolved.Anything you'd like to highlight / discuss:
Testing instructions:
Proposed commit message: (wrap lines at 72 characters)
Fix delay to return delayed function result for use
Checklist: ☑️