attempt-async-await
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

attempt-async-await

A javascript and typescript helper that runs async await function several times with delays until it returns result or attempts ends.

Installation

npm install attempt-async-await --save

Usage

Javascript

var attempt = require('attempt-async-await');
async function methodWithError(parameter) {
 
    if (methodWithError.counter < 3) {
        methodWithError.counter++;
        throw new Error('Error');
    }
 
    return parameter + ' World!';
}
 
methodWithError.counter = 1;
 
// invokes methodWithError 5 times with delay 200ms between errors
const result = await attempt.attempt(5, 200, null, methodWithError, 'Hello');
console.log(result);

Output

Hello World!

TypeScript

import { attempt } from 'attempt-async-await';
async function methodWithError(parameter: string): Promise<string> {
 
    if (methodWithError.counter < 3) {
        methodWithError.counter++;
        throw new Error('Error');
    }
 
    return parameter + ' World!';
}
 
methodWithError.counter = 1;
 
// invokes methodWithError 5 times with delay 200ms between errors
const result: string = await attempt(5, 200, null, methodWithError, 'Hello');
console.log(result);

Output

Hello World!

Build

npm run build

Tests

npm run test

Other examples

async function methodWithError(parameter) {
    if (methodWithError.counter < 3) {
        methodWithError.counter++;
        throw new Error('Error' + methodWithError.counter);
    }
 
    return parameter;
}
 
const parameter = 'finish';
 
// run 50 times with delay 200ms
const result = await attempt(50, 200, (error) => {
    if (error.message === 'Error3') {
        // stop attempts
        return false;
    } else {
        // continue execution
        return true;
    }
}, methodWithError, parameter);

If the method always throw exception the result of the attempt will be the last exception.

async function methodWithError(parameter) {
    throw new Error('Error ' + parameter);
}

// the code throws Error and you should resolves that
const result = await attempt(5, 200, null, methodWithError, 'test');

Keywords

none

Install

npm i attempt-async-await

DownloadsWeekly Downloads

4

Version

1.0.10

License

ISC

Unpacked Size

16.6 kB

Total Files

7

Last publish

Collaborators

  • ilyau