Can't break each loop early by returning false
See original GitHub issueCurrent 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’
Desired behavior:
Cypress should break the loop when returning false
from an .each
loop.
Steps to reproduce: (app code and test code)
- Do a
get
+find
, then loop through each of them with.each
return false
at some arbitrary location, preferably inside anif
statement to properly copy my situation- Get the error above
Versions
- Cypress: 3.5.0
- Node: 12.13.0
- Chrome
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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.:
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.
So what is the workaround ?
@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)?