pause-fn

1.0.0 • Public • Published

pause-fn

npm version Build Status codecov

A Node.js module to pause/resume execution of a function

const {pause, resume} = require('pause-fn');
const pausedConsoleLog = pause(console.log);
 
pausedConsoleLog(1); //=> prints nothing
pausedConsoleLog(2, 3); //=> prints nothing
 
resume(pausedConsoleLog); //=> prints '1\n' and '2 3\n'

Installation

Use npm.

npm install pause-fn

API

const pauseFn = require('pause-fn');

pauseFn(func)

func: Function
Return: Function

It returns a Function that does nothing and always returns undefined.

let num = 0;
const countUp = () => ++num;
 
countUp(); //=> 1
num; //=> 1
 
const pausedCountUp = pauseFn(countUp);
 
pausedCountUp(); //=> undefined, not 2
num; //=> 1, not 2
 
pausedCountUp(); //=> undefined
pausedCountUp(); //=> undefined
pausedCountUp(); //=> undefined
// ...
num; //=> 1

pauseFn.pause(func)

An alias of pauseFn().

pauseFn.resume(func)

func: Function returned by pauseFn()
Return: any[]

It restores the original functionality of the paused Function, calls it with the every arguments passed while paused, and returns the return values of each calls as an Array.

const pausedMathMax = pauseFn(Math.max);
 
pausedMathMax(0); //=> undefined, not 0
pausedMathMax(Infinity, 1); //=> undefined, not Infinity
 
// The original function works as usual
Math.max(2, 3); //=> 3
 
pauseFn.resume(pausedMathMax); //=> [0, Infinity]
 
// Unlike the variable name, it's no longer paused
pausedMathMax(2, 3); //=> 3

License

ISC License © 2019 Shinnosuke Watanabe

/pause-fn/

    Package Sidebar

    Install

    npm i pause-fn

    Weekly Downloads

    12

    Version

    1.0.0

    License

    ISC

    Unpacked Size

    6.45 kB

    Total Files

    4

    Last publish

    Collaborators

    • shinnn