Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using async await on .then function with parameters

I’m doing some functional testing with mocha. I stored my functions name, parameters and sucess message in local JSON file. I am checking my database response with local JSON response.
I’m using .then and .catch everywhere. I am looking to clean up a code with async await. How I can use async await here?

it('Check Authentication', (done) => {
    readFileData('checkAuth').then(({ params, message}) => {
      login.checkAuth({ ...params })
        .then((result) => {
          assert(result.message === message);
          done();
        })
        .catch((err) => done(err));
    });
});

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Something like this. Haven’t tested it tho. Basically instead of .then() you just await the call. Note that there is async before (done) callback. .catch() can be caught with try/catch block.

it('Check Authentication', async (done) => {
       let response = await readFileData('checkAuth');
       try {
         let message = await login.checkAuth({ ...response.params }); // or w/e the response is
         // assert the message
       } (catch e) {
          // do something with the error
       }
    });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading