Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda