co/utils: remove some functions and refactor.

This commit is contained in:
Christopher Jeffrey 2017-01-08 01:34:48 -08:00
parent 0a15ebefab
commit fa8ec48bc5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 27 additions and 47 deletions

View File

@ -183,11 +183,19 @@ function cb(promise, callback) {
*/
function wait() {
return new Promise(function(resolve, reject) {
nextTick(resolve);
});
return new Promise(tick);
};
/**
* Wait for a nextTick.
* @param {Function} resolve
* @param {Function} reject
*/
function tick(resolve, reject) {
nextTick(resolve);
}
/**
* Wait for a timeout with a promise.
* @param {Number} time
@ -218,41 +226,6 @@ function wrap(resolve, reject) {
};
}
/**
* Call a function that accepts node.js
* style callbacks, wrap with a promise.
* @private
* @param {Object} ctx
* @param {Function} func
* @param {Array} args
* @returns {Promise}
*/
function _call(ctx, func, args) {
return new Promise(function(resolve, reject) {
args.push(wrap(resolve, reject));
func.apply(ctx, args);
});
}
/**
* Call a function that accepts node.js
* style callbacks, wrap with a promise.
* @param {Function} func
* @returns {Promise}
*/
function call(func) {
var args = new Array(Math.max(0, arguments.length - 1));
var i;
for (i = 1; i < arguments.length; i++)
args[i - 1] = arguments[i];
/* jshint validthis:true */
return _call(this, func, args);
}
/**
* Wrap a function that accepts node.js
* style callbacks into a function that
@ -264,13 +237,17 @@ function call(func) {
function promisify(func, ctx) {
return function() {
var self = this;
var args = new Array(arguments.length);
var i;
for (i = 0; i < arguments.length; i++)
args[i] = arguments[i];
return _call(ctx || this, func, args);
return new Promise(function(resolve, reject) {
args.push(wrap(resolve, reject));
func.apply(ctx || self, args);
});
};
}
@ -320,12 +297,12 @@ function Job(resolve, reject) {
* This drives me nuts.
*/
if (typeof window !== 'undefined') {
if (typeof window !== 'undefined' && window) {
window.onunhandledrejection = function(event) {
throw event.reason;
};
} else {
process.on('unhandledRejection', function(err, promise) {
} else if (typeof process !== 'undefined' && process) {
process.on('unhandledRejection', function(err, promise) {
throw err;
});
}
@ -344,7 +321,6 @@ exports.cb = cb;
exports.wait = wait;
exports.timeout = timeout;
exports.wrap = wrap;
exports.call = call;
exports.promisify = promisify;
exports.every = every;
exports.job = job;

View File

@ -362,7 +362,7 @@ util.isUInt32 = function isUInt32(value) {
*/
util.isInt53 = function isInt53(value) {
return util.isSafeInteger(value) && util.isInt(value);
return util.isInt(value);
};
/**
@ -372,7 +372,7 @@ util.isInt53 = function isInt53(value) {
*/
util.isUInt53 = function isUInt53(value) {
return util.isSafeInteger(value) && util.isInt(value) && value >= 0;
return util.isInt(value) && value >= 0;
};
/**
@ -579,8 +579,12 @@ util.random = function random(min, max) {
util.nonce = function _nonce() {
var nonce = new Buffer(8);
nonce.writeUInt32LE(util.random(0, 0x100000000), 0, true);
nonce.writeUInt32LE(util.random(0, 0x100000000), 4, true);
var a = util.random(0, 0x100000000);
var b = util.random(0, 0x100000000);
nonce.writeUInt32LE(a, 0, true);
nonce.writeUInt32LE(b, 4, true);
return nonce;
};