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

116
Vistas
Best way to handle value tied to conditional

In a function I have I try to take a value, and if the value ends up as undefined, then I call the same function but with a different parameter

let price = await getPrice(name, category, '1');

if (price === undefined) {
   price = await getPrice(name, category, '2');

   if (price === undefined) {
      price = await getPrice(name, category, '3');
   }
}

I'm told that this approach can be very error prone, and after a bit of analyzing, I can see why. However, I don't know if there's a better way to handle this.

I need the third parameter since I use it to process a URL, as that URL has one of those strings.

static getPrice(productName, productCategory, variantNumber) {
 ...
 let url = `https://exampleurl.com/${productName}cat=${productCategory}${variantNumber}`;
 ...
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use a loop and stop when you get a defined result.

const variants = ['1', '2', '3'];
let price;
for (let i = 0; i < variants.length; i++) {
  price = await getPrice(name, category, variants[i]);
  if (price !== undefined) {
    break;
  }
}
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