question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't break each loop early by returning false

See original GitHub issue

Current behavior:

When running an .each loop and attempting to break the loop by doing return false;, Cypress doesn’t stop the loop, but instead explodes with:

CypressError: cy.then() failed because you are mixing up async and sync code.

In your callback function you invoked 1 or more cy commands but then returned a synchronous value.

Cypress commands are asynchronous and it doesn’t make sense to queue cy commands and yet return a synchronous value.

You likely forgot to properly chain the cy commands using another cy.then().

The value you synchronously returned was: ‘false’

image

Desired behavior:

Cypress should break the loop when returning false from an .each loop.

Steps to reproduce: (app code and test code)

  1. Do a get + find, then loop through each of them with .each
  2. return false at some arbitrary location, preferably inside an if statement to properly copy my situation
  3. Get the error above

Versions

  • Cypress: 3.5.0
  • Node: 12.13.0
  • Chrome

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
begli80commented, May 4, 2020

@jennifer-shehane What is the point of below if I am not able to perform any operations in my if block and only then break out of loop based on the condition.:

it('.each() - iterate over an array of elements', () => {
    cy.visit('https://example.cypress.io/commands/connectors')
    cy.get('.connectors-each-ul>li')
      .each(($el, index, $list) => {
        console.log(index)
        if (index === 1) {
          return false
        }
      })
  })

I have got the similar situation like chkashif167 below , where Im trying to break out of the each loop only after some operation based on the condition.But getting message :'cy.then() failed because you are mixing up async and sync code.

cy.get('.checkbox').each(($el, index, $list)=>{
        let checkBox=$el.prop('checked')
        if(checkBox&&index===0)
        {
            email.getPharmacyHealthOptionsPhone().eq(index).uncheck()
            email.getPharmacyHealthOptionsPhone().eq(1).check()  
            return false  
        }
})

So what is the workaround ?

1reaction
anthonychernenkocommented, Dec 11, 2019

@jennifer-shehane what should we do if need to run some async code inside .each and immediately break (like here https://github.com/cypress-io/cypress/issues/5485#issuecomment-562545976)?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't I break the each loop in cypress by returning false?
You have two each() loops. I have added an abort variable to allow the outer loop to be exited early as well
Read more >
Why you can't break a forEach loop in JavaScript
It's because the loop is running that callback function over every item, so even if you write a return it's only returning on...
Read more >
break - JavaScript - MDN Web Docs - Mozilla
The break statement terminates the current loop, switch, or label statement and transfers program control to the statement following the ...
Read more >
How to Break Out of a JavaScript forEach() Loop - Mastering JS
With every() , return false is equivalent to a break , and return true is equivalent to a continue . Another alternative is...
Read more >
each - Cypress Documentation
Iterate through an array like structure (arrays or objects with a length ... You can stop the .each() loop early by returning false...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found