0

I am calling multiple functions after receiving response.The other functions are executed before earlier functions.So I've set timer to stop the execution.But I would like to refactor it with asynchronous functions.

const testStaticData = [Ptesting, P1, P2]
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs));
if (testStaticData[0] == 'PTesting') {
    describe('API Testing', function () {
        this.timeout(5000); // How long to wait for a response (ms)
        before(function () {
            logger.info(' Test cases started');
        });
        after(function () {
            logger.info(' Test cases completed');
        });
        it('Testing for first scenario', function () {
            return chai.request(requestURL)
                .post('projects')
                .auth(Username, Password)
                .send({
                    'projectName': 'Testing123',
                })
                .then(function (res) {
                    var projectCode = res.body.code
                    console.log('project added successfully   ' + projectCode)
                    expect(res).to.have.status(200);
                    addProjectCode(projectCode)

                })
            function addProjectCode(projectCode) {
                sleep(5000).then(() => {
                chai.request(requestURL)
                    .post('projects/' + projectCode + '/details')
                    .set('content-type', 'application/json')
                    .auth(Username, Password)
                    .send({
                        'details': 'Project details',
                    })
                    .then(function (res) {
                        var newCode = res.body.code
                        expect(res).to.have.status(200);
                        addName(projectCode, newCode)

                    })
                })  
            }
        })
    })
}

How can this be refactored instead of using timer?

BTVS
  • 21
  • 4

0 Answers0