Wait for multiple Promise to return data in Cypress

Viewed 374

I have below given code in a javascript file,

class APIMessage
{
    isReady()
        {
            return cy.request({
                method: 'GET',
                url: "http://127.0.0.1:5000/isready"
            
            }).then(res => {
                return new Promise((resolve) => {
                    resolve(res);
                })
            })
        }

    receiveMsg()
    {
        return cy.request({
            method: 'GET',
            url: "http://127.0.0.1:5000/getMsg"
            
        }).then(res => {
            return new Promise((resolve) => {
                resolve(res);
            })
        })
    }

    async GetMessage()
        {
            const status =  await this.isReady()
            
            if(status.body.send == true)
            {
                var message =  await this.receiveMsg()
                cy.log("-- ",message)
            }
        }
}
var msg = new APIMessage()
export default msg

this GetMessage() I am able to import to it block of cypress, and use as

it("Receive Message",function(){
        msg.GetMessage()
    })

the test case calls GetMessage(), gets isReady status, enters if block but fails to print message, even though the API works fine, also it ends the test case successfully here without executing remaining statements.

1 Answers

Thats because you are sending a new promise when the request promise is completed. status is just another promise when the initial promise is resolved.

Just drop the extra promise.

isReady()
{
    return cy.request({
        method: 'GET',
        url: "http://127.0.0.1:5000/isready"
    });
}

receiveMsg()
{
    return cy.request({
        method: 'GET',
        url: "http://127.0.0.1:5000/getMsg"       
    });
}
Related