Cypress how to set a global variable from inside a then function

  asynchronous, cypress, javascript, jquery, promise

I need to update a global variable of a call from inside a then function. Can some one help me on how to achieve it. Below is the code snippet:

let valBefore=-1
let valAfter=-1

class abc{

  helperMethod(cntr){
    if(cntr>0){
      cy.get(C).find(D).then(($el)=>{
        return $el.length //NOT ABLE TO RETURN THIS VALUE
      })
    }
  }

  method1(){
    cy.get(A).then(($elA)=>{
      var cntr = $elA.length
      valBefore = this.helperMethod1(cntr) //NOT ABLE TO GET THE UPDATED VALUE
    })
    cy.log(valBefore) //NOT ABLE TO GET THE UPDATED VALUE HERE AS WELL
  }

  method2(){
    cy.get(B).then(($elB)=>{
      var cntr = $elB.length
      valAfter = this.helperMethod1(cntr) //NOT ABLE TO GET THE UPDATE VALUE
    })
    cy.log(valAfter) //NOT ABLE TO GET THE UPDATED VALUE HERE AS WELL
  }

  

}

Source: Ask Javascript Questions

LEAVE A COMMENT