promised-runnable
v0.2.3
Published
Configurable object for performing operation.
Downloads
8
Maintainers
Readme
promised-runnable
Configurable object for performing operation.
The source code is available on GitHub where you can also find our issue tracker.
Installation
Run the command below to install the package.
$ npm install --save promised-runnable
Getting Started
To make the code clean, the examples are written in TypeScript.
Let's start with the simplest Runnable example.
import { Runnable } from "promised-runnable";
const runnable = new Runnable({
action: () => "Done!", // Executable code block.
});
runnable.perform().then(() => {
console.log(err); // -> "Done!"
}).catch((err) => {
console.log(err);
});
Runnable can delay the execution.
const runnable = new Runnable({
action: () => "Done!",
delay: 1000, // delay execution for 1s
});
Runnable can retry the execution if it fails.
const runnable = new Runnable({
action: () => "Done!",
retries: 5, // retry 5x before failing
retryDelay: 1000, // delay the execution between retries
});
Runnable can handle timeouts.
const runnable = new Runnable({
action: () => "Done!",
timeout: 5000, // throws the RunnableTimeoutError if the execution takes more then 5s
});
Runnable can schedule the execution at a different time in the future. To give this feature more sence, let's define a new runnable object which can execute up to 1 command per second.
/** Schedule */
const Redis = require("ioredis");
const { Quota } = require("ioredis-quota");
const redis = new Redis();
const quota = new Quota({ redis });
const schedule = () => quota.schedule({
key: 'test-done',
unit: 'second',
limit: 1
});
/** Runnable */
const { Runnable } = require(".");
(async function() {
const runnable = new Runnable({
action: () => "Done!",
schedule, // limit execution to 1 per second
});
for (let i = 0; i < 10; i++) {
runnable.perform().then((d) => {
console.log(d); // -> "Done!"
}).catch((e) => {
console.log(e);
});
}
})();
Runnable execution can be canceled.
runnable.cancel();
License (MIT)
Copyright (c) 2016+ Kristijan Sedlak <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated modelation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.