Skip to content

Commit

Permalink
v4.0.0
Browse files Browse the repository at this point in the history
- Removes custom callbacks (use  instead)
- Adds  option to control how mutliple callback arguments are handled
- Update dependencies
  • Loading branch information
mikehall314 committed Apr 1, 2016
1 parent a93128b commit 715904d
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 294 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
/node_modules
.DS_Store
18 changes: 17 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
language: node_js
node_js:
- "0.10"
- "5"
- "5.10"
- "5.9"
- "5.8"
- "5.7"
- "5.6"
- "5.5"
- "5.4"
- "5.3"
- "5.2"
- "5.1"
- "4"
- "4.4"
- "4.3"
- "4.2"
- "4.1"
- "0.12"
- "0.10"
- "iojs"
64 changes: 33 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ npm install --save es6-promisify
"use strict";

// Declare variables
var promisify = require("es6-promisify"),
fs = require("fs"),
const promisify = require("es6-promisify");
const fs = require("fs");

// Convert the stat function
stat = promisify(fs.stat);
const stat = promisify(fs.stat);

// Now usable as a promise!
stat("example.txt").then(function (stats) {
Expand All @@ -32,49 +32,51 @@ stat("example.txt").then(function (stats) {
});
```

## Provide your own callback
## Promisify methods
```js
"use strict";

// Declare variables
var promisify = require("es6-promisify"),
fs = require("fs"),
stat;

// Convert the stat function, with a custom callback
stat = promisify(fs.stat, function (err, result) {
if (err) {
console.error(err);
return this.reject("Could not stat file");
}
this.resolve(result);
});
const promisify = require("es6-promisify");
const redis = require("redis").createClient(6379, "localhost");

stat("example.txt").then(function (stats) {
console.log("Got stats", stats);
// Create a promise-based version of send_command
const client = promisify(redis.send_command, redis);

// Send commands to redis and get a promise back
client("ping").then(function (pong) {
console.log("Got", pong);
}).catch(function (err) {
// err = "Could not stat file"
console.error("Unexpected error", err);
}).then(function () {
redis.quit();
});
```

## Promisify methods
## Handle callback multiple arguments
```js
"use strict";

// Declare functions
function test(cb) {
return cb(undefined, 1, 2, 3);
}

// Declare variables
var promisify = require("es6-promisify"),
redis = require("redis").createClient(6379, "localhost"),
const promisify = require("es6-promisify");

// Create a promise-based version of send_command
client = promisify(redis.send_command.bind(redis));
// Create promise-based version of test
const single = promisify(test);
const multi = promisify(test, {multiArgs: true});

// Send commands to redis and get a promise back
client("ping", []).then(function (pong) {
console.log("Got", pong);
}).catch(function (err) {
console.error("Unexpected error", err);
}).then(function () {
redis.quit();
// Discards additional arguments
single().then(function (result) {
console.log(result); // 1
});

// Returns all arguments as an array
multi().then(function (result) {
console.log(result); // [1, 2, 3]
});
```

Expand Down
23 changes: 12 additions & 11 deletions dist/promise.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

/*jslint es6, node, browser, maxlen: 120 */
/*global self, window */

"use strict";

module.exports = (function () {
module.exports = function () {

"use strict";

var globalObject = undefined;
var globalObject = void 0;

function isFunction(x) {
return typeof x === "function";
Expand All @@ -26,7 +26,7 @@ module.exports = (function () {
// implementation appears to conform to the specificaton.
// This code mostly nicked from the es6-promise module polyfill
// and then fooled with.
var hasPromiseSupport = (function () {
var hasPromiseSupport = function () {

// No promise object at all, and it's a non-starter
if (!globalObject.hasOwnProperty("Promise")) {
Expand All @@ -48,10 +48,11 @@ module.exports = (function () {

// Older version of the spec had a resolver object
// as the arg rather than a function
return (function () {
return function () {

var resolve = void 0;

var resolve = undefined,
p = new globalObject.Promise(function (r) {
var p = new globalObject.Promise(function (r) {
resolve = r;
});

Expand All @@ -60,8 +61,8 @@ module.exports = (function () {
}

return false;
})();
})();
}();
}();

// Export the native Promise implementation if it
// looks like it matches the spec
Expand All @@ -71,4 +72,4 @@ module.exports = (function () {

// Otherwise, return the es6-promise polyfill by @jaffathecake.
return require("es6-promise").Promise;
})();
}();
98 changes: 38 additions & 60 deletions dist/promisify.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,67 @@
/*jslint node, this, es6, maxlen: 120 */
"use strict";

module.exports = (function () {
/*jslint node, this, es6, maxlen: 120 */
module.exports = function () {

"use strict";

// Get a promise object. This may be native, or it may be polyfilled
var ES6Promise = require("./promise.js");

// Promise Context object constructor.
function Context(resolve, reject, custom) {
this.resolve = resolve;
this.reject = reject;
this.custom = custom;
}

// Default callback function - rejects on truthy error, otherwise resolves
function callback() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

var ctx = args.shift(),
err = args.shift(),
cust = undefined;

args = args.length > 1 ? args : args[0];

if (typeof ctx.custom === 'function') {
cust = function () {
for (var _len2 = arguments.length, custArgs = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
custArgs[_key2] = arguments[_key2];
}

// Bind the callback to itself, so the resolve and reject
// properties that we bound are available to the callback.
// Then we push it onto the end of the arguments array.
return ctx.custom.apply(cust, custArgs);
};
cust.resolve = ctx.resolve;
cust.reject = ctx.reject;
cust.call(null, err, args);
} else {
if (err) {
return ctx.reject(err);
}
ctx.resolve(args);
}
}
var ES6Promise = require("./promise.js");

/**
* promisify
* promisify()
*
* Transforms callback-based function -- func(arg1, arg2 .. argN, callback) -- into
* an ES6-compatible Promise. User can provide their own callback function; otherwise
* promisify provides a callback of the form (error, result) and rejects on truthy error.
* If supplying your own callback function, use this.resolve() and this.reject().
* an ES6-compatible Promise. Promisify provides a default callback of the form (error, result)
* and rejects when `error` is truthy. You can also supply settings object as the second argument.
*
* @param {function} original - The function to promisify
* @param {function} callback - Optional custom callbac function
*
* @return {function} A promisified version of 'original'
* @param {object} settings - Settings object
* @param {object} settings.thisArg - A `this` context to use. If not set, assume `settings` _is_ `thisArg`
* @param {bool} settings.multiArgs - Should multiple arguments be returned as an array?
* @return {function} A promisified version of `original`
*/
return function (original, custom) {
return function promisify(original, settings) {

return function () {
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}

// Store original context
var that = this;
var returnMultipleArguments = settings && settings.multiArgs;

var that = undefined;
if (settings && settings.thisArg) {
that = settings.thisArg;
} else if (settings) {
that = settings;
}

// Return the promisified function
return new ES6Promise(function (resolve, reject) {

// Create a Context object
var ctx = new Context(resolve, reject, custom);

// Append the callback bound to the context
args.push(callback.bind(null, ctx));
args.push(function callback(err) {

if (err) {
return reject(err);
}

for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}

if (false === !!returnMultipleArguments) {
return resolve(args[0]);
}

resolve(args);
});

// Call the function
original.apply(that, args);
});
};
};
})();
}();
6 changes: 3 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

"use strict";

var gulp = require("gulp"),
babel = require("gulp-babel");
var gulp = require("gulp");
var babel = require("gulp-babel");

// Compile javascript with optional minification
gulp.task("scripts", function () {
return gulp.src(["./lib/promisify.js", "./lib/promise.js"])
.pipe(babel())
.pipe(babel({presets: ["es2015"]}))
.pipe(gulp.dest("dist"));
});

Expand Down
11 changes: 6 additions & 5 deletions lib/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = (function () {
}

// There is a Promise object. Does it conform to the spec?
let P = globalObject.Promise;
const P = globalObject.Promise;

// Some of these methods are missing from
// Firefox/Chrome experimental implementations
Expand All @@ -48,10 +48,11 @@ module.exports = (function () {
// as the arg rather than a function
return (function () {

let resolve,
p = new globalObject.Promise(function (r) {
resolve = r;
});
let resolve;

const p = new globalObject.Promise(function (r) {
resolve = r;
});

if (p) {
return isFunction(resolve);
Expand Down
Loading

0 comments on commit 715904d

Please sign in to comment.