Skip to content

fre2d0m/plunger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plunger

A simple retry operation javascript library

Feature

  • Provides easy support for scene where there are operations that require retrying

Install

npm i plunger

Usage

import Plunger from 'func-plunger';

const retryCount = 3;

const plunger = new Plunger((accept, reject) => {
    device.readSensorData(deviceId, payload, (err: string, data: any) => {
        if (err) {
            return reject(err);
        }
        try {
            const buf = new Buffer(data.rs485, 'hex');
            const result = {
                humidity: buf.readInt16BE(3) / 10,
                temperature: buf.readInt16BE(5) / 10,
                lightIntensity: buf.readInt32BE(13)
            };
            accept(result);
        } catch (e) {
            reject('Failed to parse sensor data: ' + e.message);
        }
    });
});
plunger.on('failed', (errmsg: string) => {
    console.error("Error:", errmsg);
});
plunger.on('completed', (result: any) => {
    console.log("Completed!", result);
});
plunger.plunge(retryCount);

Events

  • failed: Emit the last failed error message after always failing
  • completed: After the function accept is executed once, it is considered to be completed.

About

A simple retry operation javascript library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published