-
Notifications
You must be signed in to change notification settings - Fork 5
retryPromise: improve flow and avoid bad pattern #94
Conversation
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.
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.
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 { |
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.
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'); |
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.
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) { |
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.
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]) |
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.
there should not be a timeout on the operation itself
* rewrite retry util * move test setup to script redundant with #94
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 {}
, butinstanceof TimeoutError
is false for IE11/Edge.