Package Exports
- ambi
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ambi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ambi
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
- Install:
npm install --save ambi - Use:
require('ambi')
- Install:
npm install --save ambi - Use:
require('ambi') - CDN URL:
//wzrd.in/bundle/ambi@2.4.0
- Install:
ender add ambi - Use:
require('ambi')
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 normalNotes
- Ambi accepts the arguments
(method, ...args)methodis the function to execute...argsis 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
errargument - If the synchronous method executes without error, we'll try to call the completion callback with a
errargument equal to null, and aresultargument equal to the returned result of the synchronous method
- If the synchronous method throws an error or returns an error, we'll try to call the completion callback with a single
- Ambi can also introspect a different method than the one it fires, by passing
[methodToFire, methodToIntrospect]as themethodargument
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?
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:
- Copyright © 2013+ Bevry Pty Ltd
- Copyright © 2011-2012 Benjamin Lupton
and licensed under: