Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including reverse-execution with all npm packages installed. Try it out:

var reverseExecution = require("reverse-execution")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

reverse-execution v1.0.5

Function execution with reverse parameters

reverse-execution

npm package

Inspiration

Sometimes we need to convert from a callback function to Promise and we need to change the parameter ordering of their's callback. Because Promise's callback use second parameter to handle error and Callback-style is backwards. So I decided to write this function to help us.

Example:

var p = new Promise((resolve, reject) => {
  something.asyncOperation((err, result) => {
    if (err) {
      return reject(err);
    }
    
    return resolve(result);
  })
})

Now it's easier:

var p = new Promise(callback => something.asyncOperation(inverse(callback)));

Installation

npm install reverse-execution

Example:

// Normal function
function concatWord () {
    console.log(this.foo);
    const words = Array.prototype.slice.call(arguments);
    return words.join('_');
}
    
concatWord('one', 'two', 'three'); // return 'one_two_three'

const concatWord_RE = inverse(concatWord)
concatWord_RE('one', 'two', 'three'); // return 'three_two_one';

Bugs?

This is my first public package on NPM. If you found any bugs please create a pull request. Many thanks :)

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free