Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/codeman99/callpack
Pack multi-value callback results into a single argument.
https://github.com/codeman99/callpack
callback
Last synced: about 1 month ago
JSON representation
Pack multi-value callback results into a single argument.
- Host: GitHub
- URL: https://github.com/codeman99/callpack
- Owner: CodeMan99
- License: isc
- Created: 2016-08-27T22:50:08.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-14T06:37:14.000Z (over 6 years ago)
- Last Synced: 2024-10-09T09:47:34.884Z (about 1 month ago)
- Topics: callback
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
callpack [![Build Status](https://travis-ci.org/CodeMan99/callpack.svg?branch=master)](https://travis-ci.org/CodeMan99/callpack)
========Simply wraps a callback to "pack" multiple values into a single object.
Usage
-----Callpack packs values into an array-like object by default.
callpack(function(err, result) {
console.log(result[0] === 1); // true
console.log(result[1] === 2); // true
console.log(result.length); // 2
console.log(Array.isArray(result)); // false
console.log(Array.from(result)); // [1, 2]
console.log(result.toString()); // "[object Pack]"
})(null, 1, 2);Callpack packs values into a simple object when you provide names.
callpack(function(err, result) {
console.log(result.first); // "Bill"
console.log(result.second); // "Thornton"
}, 'first', 'second')(null, 'Bill', 'Thornton');Realistic example use case.
var async = require('async');
var callpack = require('callpack');
var fs = require('fs');
var request = require('request');async.auto({
'page': cb => request('http://www.google.com', callpack(cb, 'response', 'body')),
'save': ['page', (result, cb) => {
if (result.page.response.statusCode == 200) {
fs.writeFile('./index.html', result.page.body, cb);
} else {
cb(result.page.response.statusMessage);
}
}]
});Promisifying a callback library.
var callpack = require('callpack');
var promisify = require('es6-promisify');
var _request = require('request');
var request = promisify(function() {
var cbIndex = arguments.length - 1;
arguments[cbIndex] = callpack(arguments[cbIndex], 'response', 'body');_request.apply(_request, arguments);
});request('http://www.google.com').then(result => console.log(result.body), console.error);
Reasoning
---------Consuming an asynchronous function allows for flexibility, but often tools like
the awesome async library are easier to use with only a single value. How the
function is consumed should be up to the developer, not the library.Thus callpack doesn't make any assumptions like "multiple values means an array".
Instead the decision is still up the end developer. You may use the result from
callpack as array, convert into an array, or use it to get an object that
closely mimics spreading the arguments over a function.