全部重试后,异步等待重试日志

时间:2019-10-23 13:12:05

标签: javascript node.js asynchronous async-await

所有重试完成后,如果console.log仍然失败,该如何处理?和console.log(如果成功),使用async-retry软件包:

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

如何实现?

或者换句话说,只有在所有重试尝试均失败后,才能调用另一个功能(功能A )?如果根本没有抛出功能B ,则调用它。

3 个答案:

答案 0 :(得分:0)

您是否尝试过以下方法:

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

答案 1 :(得分:0)

 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 !')

      }

    }

答案 2 :(得分:0)

您可以使用onRetry函数,类似这样

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

async function updateDB(updateUser) {
    await retry(async () => {
        const res = await test();
        console.log('Success');
    }, {
        onRetry: (err, number) => {
            console.log('attempt', number)
            if (number === 5) {
                console.log(err)
            }
        },
        retries: 5
    });
}