Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
How lambda function parameter is passed to function body

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda