Comparing version 0.0.8 to 4.2.1
'use strict'; | ||
var util = require('util'); | ||
function isErrorClass(type) { | ||
@@ -15,44 +13,61 @@ while (type && (type !== Object)) { | ||
var Promish = function(f) { | ||
if (f instanceof Promise) { | ||
// if f is a Promise, then wrap it | ||
this.__proto__ = f; | ||
} else if (f.then instanceof Function) { | ||
// if f is a 3rd Party Promise, then wrap it | ||
this.__proto__ = new Promise(function(resolve, reject) { | ||
f.then(function(value) { | ||
resolve(value); | ||
}) | ||
.catch(function(error) { | ||
reject(error); | ||
}); | ||
}); | ||
} else if (f instanceof Error) { | ||
// sugar for 'rethrow' | ||
this.__proto__ = new Promise(function(resolve, reject) { | ||
reject(f); | ||
}); | ||
} else if (f instanceof Function) { | ||
// otherwise create a new Promise and wrap that | ||
this.__proto__ = new Promise(f); | ||
} else { | ||
// resolve instantly | ||
this.__proto__ = new Promise(function(resolve, reject) { | ||
resolve(f); | ||
}); | ||
class Promish { | ||
constructor(f) { | ||
if (f instanceof Promish) { | ||
return f; | ||
} else if ((f instanceof Promise) || (f.then instanceof Function)) { | ||
// wraping other promise | ||
this.inner = f; | ||
} else if (f instanceof Error) { | ||
// sugar for 'rethrow' | ||
this.inner = new Promise((resolve,reject) => reject(f)); | ||
} else if (f instanceof Function) { | ||
this.inner = new Promise(f); | ||
} else { | ||
// anything else, resolve with value | ||
this.inner = new Promise(resolve => resolve(f)); | ||
} | ||
} | ||
// add finally sugar if not supported by Promise | ||
if (!this.finally) { | ||
this.finally = function(h) { | ||
function fin() { return h(); } | ||
return this.then(fin, fin); | ||
}; | ||
then(t, r) { | ||
var self = this; | ||
return new Promish(function(resolve, reject) { | ||
self.inner.then( | ||
function (value) { | ||
if (t) { | ||
try { | ||
resolve(t(value)); | ||
} | ||
catch(e) { | ||
reject(e); | ||
} | ||
} else { | ||
resolve(value); | ||
} | ||
}, | ||
function(error) { | ||
if (r) { | ||
try { | ||
//console.log('calling onReject with ', error) | ||
resolve(r(error)); | ||
} | ||
catch(e) { | ||
//console.log('caught handler exception', e) | ||
reject(e); | ||
} | ||
} else { | ||
reject(error); | ||
} | ||
} | ||
); | ||
}); | ||
} | ||
finally(h) { | ||
function fin() { return h(); } | ||
return this.then(fin, fin); | ||
} | ||
// extend catch to allow type specific | ||
// the last argument is the handler, all previous are matchers | ||
this.catch = function() { | ||
var args = []; | ||
Array.prototype.push.apply(args, arguments); | ||
catch() { | ||
var args = Array.from(arguments); | ||
var h = args.pop(); | ||
@@ -65,2 +80,3 @@ return this.then(undefined, function(error) { | ||
//console.log('catch matcher', error) | ||
// search for a match in argument order and return handler result if found | ||
@@ -74,3 +90,5 @@ for (var i = 0; i < args.length; i++) { | ||
} else if (matcher instanceof Function) { | ||
//console.log('matcher function') | ||
if (matcher(error)) { | ||
//console.log('matched!!') | ||
return h(error); | ||
@@ -84,12 +102,5 @@ } | ||
}); | ||
}; | ||
} | ||
// wrap then so that we maintain the Promish chain | ||
this.then = function(t, r) { | ||
var p = Promise.prototype.then.call(this, t, r); | ||
return new Promish(p); | ||
}; | ||
// sleep some ms then resolve with incoming value | ||
this.delay = function(timeout) { | ||
delay(timeout) { | ||
return this.then(function(value) { | ||
@@ -102,5 +113,5 @@ return new Promish(function(resolve) { | ||
}); | ||
}; | ||
} | ||
this.spread = function(f) { | ||
spread(f) { | ||
return this.then(function(value){ | ||
@@ -110,170 +121,182 @@ return f.apply(undefined, value); | ||
} | ||
} | ||
// Wrap Promise.all | ||
static all(promises) { | ||
return new Promish(Promise.all(promises)); | ||
} | ||
// Provide 'Promish' version | ||
Promish.resolve = function(value) { | ||
return new Promish(function(resolve, reject) { | ||
resolve(value); | ||
}); | ||
} | ||
// Turn a value into a promish | ||
static resolve(value) { | ||
return new Promish(resolve => resolve(value)); | ||
} | ||
// Turn an error into a rejected promish | ||
static reject(error) { | ||
return new Promish((resolve,reject) => reject(error)); | ||
} | ||
// Wrap a synchronous method and resolve with its return value | ||
static method(f) { | ||
return function() { | ||
var self = this; // is this necessary? | ||
var args = Array.from(arguments); | ||
return new Promish(resolve => resolve(f.apply(self, args))); | ||
} | ||
} | ||
// Provide 'Promish' version | ||
Promish.reject = function(error) { | ||
return new Promish(function(resolve, reject) { | ||
reject(error); | ||
}); | ||
} | ||
// Wrap a synchronous method and resolve with its return value | ||
Promish.method = function(f) { | ||
return function() { | ||
var self = this; | ||
var args = arguments; | ||
// Wrap Promise.race | ||
static race(promises) { | ||
return new Promish(Promise.race(promises)); | ||
}; | ||
// | ||
static apply(f, args) { | ||
// take a copy of args because a) might not be Array and b) no side-effects | ||
args = Array.from(args); | ||
return new Promish(function(resolve, reject) { | ||
resolve(f.apply(self, args)); | ||
}); | ||
} | ||
} | ||
// Promise.all already supported but need to wrap in Promish | ||
Promish.all = function(promises) { | ||
return new Promish(Promise.all(promises)); | ||
} | ||
// same for Promise.race | ||
Promish.race = Promise.race || function(promises) { | ||
return new Promish(Promise.race(promises)); | ||
}; | ||
Promish.apply = Promish.nfapply = function(f, args) { | ||
// take a copy of args because a) might not be Array and b) no side-effects | ||
args = Array.prototype.slice.call(args); | ||
return new Promish(function(resolve, reject) { | ||
args.push(function () { | ||
var error = Array.prototype.shift.apply(arguments); | ||
if (error) { | ||
reject(error); | ||
} else { | ||
if (arguments.length === 1) { | ||
resolve(arguments[0]); | ||
args.push(function () { | ||
var error = Array.prototype.shift.apply(arguments); | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(arguments); | ||
if (arguments.length === 1) { | ||
resolve(arguments[0]); | ||
} else { | ||
resolve(arguments); | ||
} | ||
} | ||
} | ||
}); | ||
f.apply(undefined, args); | ||
}); | ||
f.apply(undefined, args); | ||
}); | ||
} | ||
Promish.call = Promish.nfcall = function() { | ||
var f = Array.prototype.shift.apply(arguments); | ||
return Promish.apply(f, arguments); | ||
} | ||
Promish.post = Promish.npost = function(o, f, a) { | ||
return Promish.apply(f.bind(o), a); | ||
} | ||
Promish.invoke = Promish.ninvoke = function() { | ||
var o = Array.prototype.shift.apply(arguments); | ||
var f = Array.prototype.shift.apply(arguments); | ||
return Promish.apply(f.bind(o), arguments); | ||
} | ||
// create curry function for nfcall | ||
Promish.promisify = Promish.denodify = function(f) { | ||
return function() { | ||
} | ||
static nfapply(f, args) { | ||
return Promish.apply(f, args); | ||
} | ||
static call() { | ||
var f = Array.prototype.shift.apply(arguments); | ||
return Promish.apply(f, arguments); | ||
} | ||
} | ||
// create Q based curry function for ninvoke | ||
Promish.nbind = function(f, o) { | ||
// Why is it function, object? | ||
return function() { | ||
return Promish.post(o, f, arguments); | ||
static nfcall() { | ||
return Promish.call.apply(null, arguments); | ||
} | ||
} | ||
// curry function for ninvoke with arguments in object, method order | ||
Promish.bind = function(o, f) { | ||
return function() { | ||
return Promish.post(o, f, arguments); | ||
static post(o, f, a) { | ||
return Promish.apply(f.bind(o), a); | ||
} | ||
} | ||
// Promishify every method in an object | ||
Promish.promisifyAll = function(o, options) { | ||
options = options || {}; | ||
var inPlace = options.inPlace || false; | ||
var suffix = options.suffix || (inPlace ? 'Async' : ''); | ||
static npost(o,f,a) { | ||
return Promish.apply(f.bind(o), a); | ||
} | ||
var p = {}; | ||
var oo = o; | ||
while (oo && (oo !== Object)) { | ||
for (var i in oo) { | ||
var value = oo[i]; | ||
if (!p[i + suffix] && (value instanceof Function)) { | ||
p[i + suffix] = Promish.bind(o, value); | ||
} | ||
static invoke() { | ||
var o = Array.prototype.shift.apply(arguments); | ||
var f = Array.prototype.shift.apply(arguments); | ||
return Promish.apply(f.bind(o), arguments); | ||
} | ||
static ninvoke() { | ||
return Promish.invoke(arguments); | ||
} | ||
// create curry function for nfcall | ||
static promisify(f) { | ||
return function() { | ||
return Promish.apply(f, arguments); | ||
} | ||
oo = oo.__proto__ || oo.prototype; | ||
} | ||
static denodify(f) { | ||
return Promish.promisify(f); | ||
} | ||
if (inPlace) { | ||
for (var i in p) { | ||
o[i] = p[i]; | ||
// create Q based curry function for ninvoke | ||
static nbind(f, o) { | ||
// Why is it function, object and not object, function like the others? | ||
return function() { | ||
return Promish.post(o, f, arguments); | ||
} | ||
p = o; | ||
} else { | ||
// waiting for ES6 Proxies! | ||
} | ||
return p; | ||
} | ||
// curry function for ninvoke with arguments in object, method order | ||
static bind(o, f) { | ||
return function() { | ||
return Promish.post(o, f, arguments); | ||
} | ||
} | ||
// some - the first n to resolve, win - else reject with all of the errors | ||
Promish.some = Promise.some || function(promises, n) { | ||
return new Promish(function(resolve, reject) { | ||
var values = []; | ||
var rejects = []; | ||
promises.forEach(function(promise) { | ||
promise | ||
.then(function(value) { | ||
values.push(value); | ||
if (values.length >= n) { | ||
resolve(values); | ||
} | ||
}) | ||
.catch(function(error) { | ||
rejects.push(error); | ||
if (rejects.length > promises.length - n){ | ||
reject(rejects); | ||
} | ||
}); | ||
// Promishify every method in an object | ||
static promisifyAll(o, options) { | ||
options = options || {}; | ||
var inPlace = options.inPlace || false; | ||
var suffix = options.suffix || (inPlace ? 'Async' : ''); | ||
var p = {}; | ||
var oo = o; | ||
while (oo && (oo !== Object)) { | ||
for (var i in oo) { | ||
var value = oo[i]; | ||
if (!p[i + suffix] && (value instanceof Function)) { | ||
p[i + suffix] = Promish.bind(o, value); | ||
} | ||
} | ||
oo = oo.__proto__ || oo.prototype; | ||
} | ||
if (inPlace) { | ||
for (var i in p) { | ||
o[i] = p[i]; | ||
} | ||
p = o; | ||
} else { | ||
// waiting for ES6 Proxies! | ||
} | ||
return p; | ||
} | ||
// some - the first n to resolve, win - else reject with all of the errors | ||
static some(promises, n) { | ||
return new Promish(function(resolve, reject) { | ||
var values = []; | ||
var rejects = []; | ||
promises.forEach(function(promise) { | ||
promise | ||
.then(function(value) { | ||
values.push(value); | ||
if (values.length >= n) { | ||
resolve(values); | ||
} | ||
}) | ||
.catch(function(error) { | ||
rejects.push(error); | ||
if (rejects.length > promises.length - n){ | ||
reject(rejects); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
// any - the first to resolve, wins - else reject with all of the errors | ||
Promish.any = Promise.any || function(promises) { | ||
return Promish.some(promises, 1) | ||
.then(function(values) { | ||
return values[0]; | ||
} | ||
// any - the first to resolve, wins - else reject with all of the errors | ||
static any(promises) { | ||
return Promish.some(promises, 1) | ||
.then(function(values) { | ||
return values[0]; | ||
}); | ||
}; | ||
// old-style for ease of adoption | ||
static defer() { | ||
var deferred = {}; | ||
deferred.promise = new Promish(function(resolve, reject) { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
}; | ||
// old-style for ease of adoption | ||
Promish.defer = function() { | ||
var deferred = {}; | ||
deferred.promise = new Promish(function(resolve, reject) { | ||
deferred.resolve = resolve; | ||
deferred.reject = reject; | ||
}); | ||
return deferred; | ||
return deferred; | ||
} | ||
// spread - apply array of values to function as args | ||
static spread(value, f) { | ||
return f.apply(undefined, value); | ||
} | ||
} | ||
// spread - apply array of values to function as args | ||
Promish.spread = function(value, f) { | ||
return f.apply(undefined, value); | ||
} | ||
module.exports = Promish; |
{ | ||
"name": "promish", | ||
"version": "0.0.8", | ||
"version": "4.2.1", | ||
"description": "ES6 Promise Shim", | ||
@@ -22,2 +22,5 @@ "private": false, | ||
], | ||
"engines" : { | ||
"node" : ">=4.0.0" | ||
}, | ||
"dependencies": { | ||
@@ -24,0 +27,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
265
1
9711
3
1
0