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 = ;
{ if methodWithErrorcounter < 3 methodWithErrorcounter++; throw 'Error'; return parameter + ' World!';} methodWithErrorcounter = 1; // invokes methodWithError 5 times with delay 200ms between errorsconst result = await attempt;console;
Output
Hello World!
TypeScript
;
async
Output
Hello World!
Build
npm run build
Tests
npm run test
Other examples
{ if methodWithErrorcounter < 3 methodWithErrorcounter++; throw 'Error' + methodWithErrorcounter; return parameter;} const parameter = 'finish'; // run 50 times with delay 200msconst result = await ;
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');