Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
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));
    });
});

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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
       }
    });
about 4 years ago · Juan Pablo Isaza Report

0

  • Changed callback function to async to use `await
  • Wrapped all await calls in try-catch block to handle errors
  • Used const for params, message and result variables but if you are going to reassign values later in the code you can use let instead.
  • done() will be async call. Add await in front of that if you need that too to be sync call

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!