I am new to javacscript and typescript, I found some code online, I do not understand it, can you explain it.
Thanks
original code https://www.learnrxjs.io/learn-rxjs/operators/error_handling/retrywhen
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
}: {
maxRetryAttempts?: number,
scalingDuration?: number,
excludedStatusCodes?: number[]
} = {}) => (attempts: Observable<any>) => {
return attempts.pipe(
mergeMap((error, i) => {
const retryAttempt = i + 1;
// if maximum number of retries have been met
// or response is a status code we don't wish to retry, throw error
if (
retryAttempt > maxRetryAttempts ||
excludedStatusCodes.find(e => e === error.status)
) {
return throwError(error);
}
console.log(
`Attempt ${retryAttempt}: retrying in ${retryAttempt *
scalingDuration}ms`
);
// retry after 1s, 2s, etc...
return timer(retryAttempt * scalingDuration);
}),
finalize(() => console.log('We are done!'))
);
};
First question, how does this piece of code work
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
}: {
maxRetryAttempts?: number,
scalingDuration?: number,
excludedStatusCodes?: number[]
} = {})
second question, there is **=> (attempts: Observable<any>)** => between code above and function body, how **maxRetryAttempts** is pass to function body.
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
}: {
maxRetryAttempts?: number,
scalingDuration?: number,
excludedStatusCodes?: number[]
} = {})
This declare a function that takes one argument. That part after the : on line 5 types that object as an object with three properties, each of which might be undefined or omitted (thats what the ? means).
In lines 2-4 we see that this object is descructured, so that each property becomes a local variable. In addition, each variable has a default value (the = 3 after the variable name) in case that property has no value on the argument object.
The last line provides a default value for the whole object, meaning you can omit it completely.
Now for this line:
//...
} = {}) => (attempts: Observable<any>) => {
In short, this function returns a function.
A simpler example:
you could write this:
const a = (x: number) => {
return (y: number) => x * y
}
const b = a(2)
b(3) // 6
const a = (x: number) => (y: number) => x * y
const b = a(2)
b(3) // 6
In plain english, a is a function which, returns a function, which returns a value.
In the case of the code in your question genericRetryStrategy takes an objects that configures how the operation will retry and returns a function. That function that takes an observable to operate on, which reties according how you first called genericRetryStrategy
So to sum up:
export const genericRetryStrategy = ({
maxRetryAttempts = 3,
scalingDuration = 1000,
excludedStatusCodes = []
}: {
maxRetryAttempts?: number,
scalingDuration?: number,
excludedStatusCodes?: number[]
} = {}) => (attempts: Observable<any>) => {
//..
}
is called like this:
const strategy = genericRetryStrategy({ maxRetryAttempts: 10 })
const doWithRetries = strategy(myObservable)