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

152
Vistas
Return a value from an async block in Cypress?

I am trying to return a value from a function where the return value is inside the then() block. Cypress throws a bunch of errors that I am mixing async and sync code. I tried returning a Promise and resolving it, but that threw up errors too.

cy.get('.btn.btn-primary')
  .each(function ($el, index, $list) {
    // Lot of code
 
    if (price < minPrice) minPrice = price
  })
  .then(() => {
    cy.log(minPrice); //This works fine
    return minPrice; //This throws ERROR
  })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use an alias to save the value and then later use it something like this.

cy.get('.btn.btn-primary')
  .each(function ($el, index, $list) {
    // Lot of code

    if (price < minPrice) minPrice = price
  })
  .then(() => {
    cy.log(minPrice) //This works fine
    cy.wrap(minPrice).as('minPrice') //Saved as alias
  })

//In your test you can use it as
cy.get('@minPrice').then((minPrice) => {
  //Access minPrice here
  cy.log(minPrice) //prints minPrice
})
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