GithubHelp home page GithubHelp logo

christav / ambi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bevry/ambi

0.0 0.0 0.0 463 KB

Execute a function ambidextrously (normalizes the differences between synchronous and asynchronous functions). Useful for treating synchronous functions as asynchronous functions (like supporting both synchronous and asynchronous event definitions automatically).

License: Other

JavaScript 100.00%

ambi's Introduction

ambi

Travis CI Build Status NPM version NPM downloads Dependency Status Dev Dependency Status
Patreon donate button Flattr donate button Liberapay donate button Thanks App donate button Boost Lab donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Execute a function ambidextrously (normalizes the differences between synchronous and asynchronous functions). Useful for treating synchronous functions as asynchronous functions (like supporting both synchronous and asynchronous event definitions automatically).

Install

NPM

  • Install: npm install --save ambi
  • Module: require('ambi')

Browserify

  • Install: npm install --save ambi
  • Module: require('ambi')
  • CDN URL: //wzrd.in/bundle/[email protected]

Ender

  • Install: ender add ambi
  • Module: require('ambi')

This package is published with the following editions:

  • ambi aliases ambi/index.js which uses Editions to automatically select the correct edition for the consumers environment
  • ambi/source/index.js is esnext source code with require for modules
  • ambi/edition-browsers/index.js is esnext compiled for browsers with require for modules
  • ambi/edition-node-0.8/index.js is esnext compiled for node.js 0.8 with require for modules

Usage

Example

// Import
const ambi = require('ambi')
let result = null

// Sample methods
function syncMethod (x, y) {
    return x * y
}
function asyncMethod (x, y, next) {
    return setTimeout(function () {
        next(null, x * y)
    }, 0)
}

// Call the synchronous function asynchronously
// ambi adds support for this asynchronous callback automatically
result = ambi(syncMethod, 5, 2, function (err, result) {
    console.log(err, result)  // null, 10
})
console.log(result)  // 10 - just like normal

// Call the asynchronous function asynchronously
// ambi doesn't do anything special here
result = ambi(asyncMethod, 5, 2, function (err, result) {
    console.log(err, result)  // null, 10
})
console.log(result)  // setTimeout - just like normal

Notes

  • Ambi accepts the arguments (method, ...args)
    • method is the function to execute
    • ...args is the arguments to send to the method
      • the last argument is expected to be the completion callback
      • the completion callback is optional, but if defined, is expected to have the signature of (err, ...results)
  • If the method has equal or more arguments than ambi received, then we assume it is an asynchronous method and let it handle calling of the completion callback itself
  • If the method has less arguments than ambi received, then we assume it is a synchronous method and we'll call the completion callback ourselves
    • If the synchronous method throws an error or returns an error, we'll try to call the completion callback with a single err argument
    • If the synchronous method executes without error, we'll try to call the completion callback with a err argument equal to null, and a result argument equal to the returned result of the synchronous method

History

Discover the release history by heading on over to the HISTORY.md file.

Contribute

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

Backers

Maintainers

These amazing people are maintaining this project:

Sponsors

No sponsors yet! Will you be the first?

Patreon donate button Flattr donate button Liberapay donate button Thanks App donate button Boost Lab donate button Buy Me A Coffee donate button Open Collective donate button crypto donate button PayPal donate button Wishlist browse button

Contributors

These amazing people have contributed code to this project:

Discover how you can contribute by heading on over to the CONTRIBUTING.md file.

License

Unless stated otherwise all works are:

and licensed under:

ambi's People

Contributors

balupton avatar sfrdmn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.