Ad

Async Await Retry Log After All Retries

How can I console.log if it still fails after all retries done? and console.log if it succeeds, using async-retry package:

    const retry = require('async-retry');

    async function updateDB(updateUser) {
        await retry(async () => {
            const res = await updateUser;
            if (/* some error after finishing all retries*/) {
                console.log('failed');
            }
            console.log('Success');
        }, {
          retries: 5
        });
    }

how can this be achieved?

or in other words, how can I call another function (function A) only after all retries attempts failed? and call function B if it didn't throw at all.

Ad

Answer

 const retry = require('async-retry');

    async function updateDB(updateUser) {
    try{ 
        const result =   await retry(async () => {
            const res = await Promise.all(updateUser).then(()=>{
              try{
                return  new Promise((resolve) => resolve('OK')),  {  retries: 5   }
              }catch(e){
                return  new Promise((resolve) => resolve('KO')),  {  retries: 5   };

              }
         });
         }
      );
    }catch(err){
        console.log('The function execution failed !')

      }

    }
Ad
source: stackoverflow.com
Ad