Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

retryPromise: improve flow and avoid bad pattern #94

Closed
wants to merge 9 commits into from

Conversation

AviVahl
Copy link
Contributor

@AviVahl AviVahl commented Oct 6, 2017

No longer creates an empty Promise without any use (async (resolve, reject)=>...).
No longer checks and rechecks whether it's in a timout.
When timeout value is provided, it creates a single delayed rejected promise and uses it with Promise.race when doing operations.
A unique object is used to identify a timeout error. First try was class TimeoutError extends Error {}, but instanceof TimeoutError is false for IE11/Edge.

No longer creates an empty Promise without any use (`async (resolve, reject)=>...`).
No longer checks and rechecks whether it's in a timout.
When timeout value is provided, it creates a single delayed rejected promise and uses it with Promise.race when doing operations.
A unique object is used to identify a timeout error. First try was `class TimeoutError extends Error {}`, but `instanceof TimeoutError` is false for IE11/Edge.
not really needed, as first run has retries equal to retriesLeft.
also now avoids delaying when interval is 0.
Use do-while inside an async function
This way, avoids always creating a rejected timeout Promise, even if operation succeeded.
Copy link
Contributor

@amir-arad amir-arad left a comment

Choose a reason for hiding this comment

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

all in all this functionality should definitely not be part of kissfs but a dependency.

reject(lastError);
let lastError: Error | undefined;
const timeoutPromise = timeout && delayedPromise(timeout).then(() => timeoutSymbol);
do {
Copy link
Contributor

Choose a reason for hiding this comment

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

the loop is over complicated. there is an initialization block hidden inside the do block.

@@ -16,30 +16,34 @@ export interface RetryPromiseOptions {
timeoutMessage?: string;
}

export function retryPromise<T>(
const timeoutSymbol = Symbol('timeout');
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to use a symbol.{} is enough.

if (timeoutPromise) {
shouldDelay && await Promise.race([delayedPromise(interval), timeoutPromise]);
const result = await Promise.race([promiseProvider(), timeoutPromise])
if (result === timeoutSymbol) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if timeoutPromise would simply return timeoutPromise it would be much simpler.

try {
if (timeoutPromise) {
shouldDelay && await Promise.race([delayedPromise(interval), timeoutPromise]);
const result = await Promise.race([promiseProvider(), timeoutPromise])
Copy link
Contributor

Choose a reason for hiding this comment

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

there should not be a timeout on the operation itself

amir-arad pushed a commit that referenced this pull request Nov 28, 2017
* rewrite retry util
* move test setup to script
redundant with #94
@amir-arad amir-arad closed this Jan 8, 2018
@amir-arad amir-arad deleted the avi/retry-promise branch April 29, 2018 10:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants