co/utils: remove some functions and refactor.
This commit is contained in:
parent
0a15ebefab
commit
fa8ec48bc5
@ -183,11 +183,19 @@ function cb(promise, callback) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
function wait() {
|
function wait() {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(tick);
|
||||||
nextTick(resolve);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait for a nextTick.
|
||||||
|
* @param {Function} resolve
|
||||||
|
* @param {Function} reject
|
||||||
|
*/
|
||||||
|
|
||||||
|
function tick(resolve, reject) {
|
||||||
|
nextTick(resolve);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait for a timeout with a promise.
|
* Wait for a timeout with a promise.
|
||||||
* @param {Number} time
|
* @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
|
* Wrap a function that accepts node.js
|
||||||
* style callbacks into a function that
|
* style callbacks into a function that
|
||||||
@ -264,13 +237,17 @@ function call(func) {
|
|||||||
|
|
||||||
function promisify(func, ctx) {
|
function promisify(func, ctx) {
|
||||||
return function() {
|
return function() {
|
||||||
|
var self = this;
|
||||||
var args = new Array(arguments.length);
|
var args = new Array(arguments.length);
|
||||||
var i;
|
var i;
|
||||||
|
|
||||||
for (i = 0; i < arguments.length; i++)
|
for (i = 0; i < arguments.length; i++)
|
||||||
args[i] = arguments[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.
|
* This drives me nuts.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined' && window) {
|
||||||
window.onunhandledrejection = function(event) {
|
window.onunhandledrejection = function(event) {
|
||||||
throw event.reason;
|
throw event.reason;
|
||||||
};
|
};
|
||||||
} else {
|
} else if (typeof process !== 'undefined' && process) {
|
||||||
process.on('unhandledRejection', function(err, promise) {
|
process.on('unhandledRejection', function(err, promise) {
|
||||||
throw err;
|
throw err;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -344,7 +321,6 @@ exports.cb = cb;
|
|||||||
exports.wait = wait;
|
exports.wait = wait;
|
||||||
exports.timeout = timeout;
|
exports.timeout = timeout;
|
||||||
exports.wrap = wrap;
|
exports.wrap = wrap;
|
||||||
exports.call = call;
|
|
||||||
exports.promisify = promisify;
|
exports.promisify = promisify;
|
||||||
exports.every = every;
|
exports.every = every;
|
||||||
exports.job = job;
|
exports.job = job;
|
||||||
|
|||||||
@ -362,7 +362,7 @@ util.isUInt32 = function isUInt32(value) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
util.isInt53 = function isInt53(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) {
|
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() {
|
util.nonce = function _nonce() {
|
||||||
var nonce = new Buffer(8);
|
var nonce = new Buffer(8);
|
||||||
nonce.writeUInt32LE(util.random(0, 0x100000000), 0, true);
|
var a = util.random(0, 0x100000000);
|
||||||
nonce.writeUInt32LE(util.random(0, 0x100000000), 4, true);
|
var b = util.random(0, 0x100000000);
|
||||||
|
|
||||||
|
nonce.writeUInt32LE(a, 0, true);
|
||||||
|
nonce.writeUInt32LE(b, 4, true);
|
||||||
|
|
||||||
return nonce;
|
return nonce;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user