Search code examples
javascriptfunctionautomated-testscypresssettimeout

Set timeout (setTimeOut) in a created function


I have a function below and I would like to set a timeout. If the execution of this function exceeds 2 minutes, timeout and the next test flow follows.

Does anyone know how I add a setTimeOut inside my function?

Function:

function resWait() {
    cy.req(
      'GET',
      `${url}/transaction/acquirertrxquery?acquirerId=1&dateFrom=${actualDate}%2000:00:00&dateTo=${actualDate}%2023:59:00&externalId=${idExternal}`
    ).then((res) => {
      if (res.body.content[0].trxConfirmationStatus.description === 'Confirmed') {
        return
      }
      cy.wait(3000)
      resWait()
    })
  }

I tried as follows setting timeout, but without success:

function resWait() {
    cy.req(
      'GET',
      `${url}/transaction/acquirertrxquery?acquirerId=1&dateFrom=${actualDate}%2000:00:00&dateTo=${actualDate}%2023:59:00&externalId=${idExternal}`
    ).then((res) => {
      if (res.body.content[0].trxConfirmationStatus.description === 'Confirmed') {
        return
      }
      cy.wait(3000)
      resWait()
    })
  }

setTimeout(resWait, 20000);

How do I create a promise for my function? I have an example below, but how do I link it to my function? And then I call the function or my promise?

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

Would you help me?

Thanks in advance!!


Solution

  • You can use promise like this in your code :

    function resWait() {
    cy.req(
      'GET',
      `${url}/transaction/acquirertrxquery?acquirerId=1&dateFrom=${actualDate}%2000:00:00&dateTo=${actualDate}%2023:59:00&externalId=${idExternal}`
    ).then((res) => {
      if (res.body.content[0].trxConfirmationStatus.description === 'Confirmed') {
        return
      }
      // After 3 sec again call function
      await sleep(3000);
      resWait(); // Call resWait function again
    })
    }
    
    function sleep(ms) {
      return new Promise(resolve => setTimeout(resolve, ms));
    }
    

    You can call promise function inside your resWait() and get outside of function, when ajax is successfully, So the promise return two reject for error and resolve for success.

    function resWait() {
      return new Promise((resolve, reject) => {
         cy.req(
          'GET',
          `${url}/transaction/acquirertrxquery?acquirerId=1&dateFrom=${actualDate}%2000:00:00&dateTo=${actualDate}%2023:59:00&externalId=${idExternal}`
      ).then((res) => {
         if (res.body.content[0].trxConfirmationStatus.description === 'Confirmed') {
           resolve('Transaction confirmed.'); 
        } else {
          reject(new Error('Transaction not confirmed.')); 
        }
       }).catch((error) => {
        reject(error); 
      });
     });
    }
    // get promise return outside of function
    resWait()
     .then((message) => {
       console.log(message); 
      
     })
    .catch((error) => {
      console.error('An error occurred:', error);
      
    });
    

    You can call promise function inside your resWait() and get inside of function.

    function resWait() {
     var promise = new Promise((resolve, reject) => {
        cy.req(
          'GET',
          `${url}/transaction/acquirertrxquery?acquirerId=1&dateFrom=${actualDate}%2000:00:00&dateTo=${actualDate}%2023:59:00&externalId=${idExternal}`
       ).then((res) => {
         if (res.body.content[0].trxConfirmationStatus.description === 'Confirmed') {
           resolve('Transaction confirmed.'); 
         } else {
           reject(new Error('Transaction not confirmed.')); 
         }
       }).catch((error) => {
        reject(error); 
      });
     });
    
     promise.then((message) => {
       console.log(message); 
       
     })
      .catch((error) => {
      console.error('An error occurred:', error);
      
     });
    }