Comparing version 0.0.5 to 0.0.6
@@ -15,6 +15,2 @@ 'use strict'; | ||
var PromishBase = function(p) { | ||
this.__proto__ = p; | ||
} | ||
var Promish = function(f) { | ||
@@ -112,2 +108,27 @@ if (f instanceof Promise) { | ||
// Provide 'Promish' version | ||
Promish.resolve = function(value) { | ||
return new Promish(function(resolve, reject) { | ||
resolve(value); | ||
}); | ||
} | ||
// 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; | ||
return new Promish(function(resolve, reject) { | ||
resolve(f.apply(self, args)); | ||
}); | ||
} | ||
} | ||
// Promise.all already supported but need to wrap in Promish | ||
@@ -118,2 +139,7 @@ Promish.all = function(promises) { | ||
// same for Promise.race | ||
Promish.race = Promise.race || function(promises) { | ||
return new Promish(Promise.race(promises)); | ||
}; | ||
Promish.apply = Promish.nfapply = function(f, args) { | ||
@@ -204,11 +230,2 @@ // take a copy of args because a) might not be Array and b) no side-effects | ||
// race - the first to fulfil, wins | ||
Promish.race = Promise.race || function(promises) { | ||
return new Promish(function(resolve, reject) { | ||
promises.each(function(promise) { | ||
promise.then(resolve).catch(reject); | ||
}); | ||
}); | ||
}; | ||
// any - the first to resolve, wins - else reject with all of the errors | ||
@@ -215,0 +232,0 @@ Promish.any = Promise.any || function(promises) { |
{ | ||
"name": "promish", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "ES6 Promise Shim", | ||
@@ -17,3 +17,6 @@ "private": false, | ||
"keywords": [ | ||
"ES6", "Promise", "Shim", "EcmaScript", "Promises", "then", "catch", "finally", "race", "all", "any" | ||
"ES6", "Promise", "Shim", "EcmaScript", "Promises", "resolve", "reject", | ||
"then", "catch", "finally", | ||
"race", "all", "any", | ||
"Promisification", "call", "apply" | ||
], | ||
@@ -20,0 +23,0 @@ "dependencies": { |
@@ -23,3 +23,3 @@ # Promish | ||
<ul> | ||
<li><a href="#promisification">Promishification</a></li> | ||
<li>Bugfixes and Documentation</li> | ||
</ul> | ||
@@ -53,2 +53,3 @@ | ||
<li><a href="#promisify-all">Promisify All</a></li> | ||
<li><a href="#method">Method</a></li> | ||
</ul> | ||
@@ -76,2 +77,3 @@ </li> | ||
### Typical use - construct with handler function | ||
```javascript | ||
@@ -84,4 +86,7 @@ var promise = new Promish(function(resolve, reject) { | ||
### 3rd Party Wrapper Mode | ||
```javascript | ||
var promise = new Promish(Q()); | ||
var promise = new Promish(new Promise( ... )); | ||
``` | ||
@@ -91,3 +96,8 @@ | ||
```javascript | ||
// If the constructor value is not a function, a thenable or an Error, | ||
// assume it should be a resolve value. | ||
var promise = new Promish('Resolve Value'); | ||
// To explicitly signal resolve, use Promish.resolve | ||
var promise = Promish.resolve('Resolve Value'); | ||
``` | ||
@@ -97,5 +107,11 @@ | ||
```javascript | ||
// If the constructor value is an Error type, it will be interpreted as rejection | ||
var promise = new Promish(new Error('This promise is rejected')); | ||
// To explicitly signal something is rejection use Promish.reject | ||
var promise = Promish.reject('This is not an error object, but reject with it anyway') | ||
``` | ||
## Then | ||
@@ -166,2 +182,3 @@ | ||
}); | ||
``` | ||
@@ -221,11 +238,2 @@ ## Finally | ||
## Spread | ||
```javascript | ||
Promish.all(getPromish1(), getPromish2(), getPromish3()) | ||
.spread(function(value1, value2, value3) { | ||
// use values... | ||
}); | ||
``` | ||
## Promisification Calls | ||
@@ -298,3 +306,3 @@ | ||
Promisify all the methods of an object. | ||
Promisify all the async methods of an object. | ||
@@ -322,5 +330,48 @@ There are two modes supported: | ||
### Method | ||
Wrap a synchronous function or method so that it always returns a promise | ||
```javascript | ||
var myFunc = Promish.method(function(value) { | ||
// can throw | ||
if (!value) throw new Error('Not zero!'); | ||
// can return value | ||
if (value > 0) return value; | ||
// can return promish() | ||
return Promish.resolve(value); | ||
}); | ||
myFunc(1234) | ||
.then(function(value) { | ||
// ... | ||
}); | ||
// also works as member functions | ||
MyClass.prototype.func = Promish.method(function(value) { | ||
// this is what you think it is | ||
return this.value = value; | ||
}); | ||
new MyClass(7).func | ||
.then(function(value) { | ||
// ... | ||
}); | ||
``` | ||
## All | ||
Promish wraps the native implementation of all. | ||
```javascript | ||
Promish.all([getPromise1(), getPromise2()]) | ||
.then(function(values) { ... }); | ||
``` | ||
## Race | ||
Resolve (or reject) on first fulfilment of an array of promises. | ||
Promish wraps the native implementation of race. | ||
@@ -358,9 +409,7 @@ ```javascript | ||
```javascript | ||
new Promish(function(resolve, reject) { | ||
resolve([1,2,3]); | ||
}) | ||
Promish.all(getPromish1(), getPromish2(), getPromish3()) | ||
.spread(function(a,b,c) { | ||
// a === 1 | ||
// b === 2 | ||
// c === 3 | ||
// a === value from getPromish1 | ||
// b === value from getPromish2 | ||
// c === value from getPromish3 | ||
}); | ||
@@ -381,2 +430,5 @@ | ||
| 0.0.3 | <ul><li><a href="#delay">Promish.delay()</li><li><a href="#defer">Promish.defer()</li><li><a href="#spread">Promish.spread()</li></ul> | | ||
| 0.0.4 | <ul><li><a href="#apply">Promish.apply()</li><li><a href="#call">Promish.call()</li></ul> | | ||
| 0.0.4 | <ul><li><a href="#apply">Promish.apply()</li><li><a href="#call">Promish.call()</li></ul> | | ||
| 0.0.5 | <ul><li><a href="#promisification">Promishification</a></li></ul> | | ||
| 0.0.6 | <ul><li>Bugfixes and Documentation</li></ul> | | ||
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
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
19336
231
423