Skip to content

Commit 7546808

Browse files
Merge branch 'deprecations'
Conflicts: index.js test/extensions-tests.js
2 parents 19b2493 + 484e430 commit 7546808

File tree

3 files changed

+62
-196
lines changed

3 files changed

+62
-196
lines changed

Readme.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
<a href="http://promises-aplus.github.com/promises-spec"><img src="http://promises-aplus.github.com/promises-spec/assets/logo-small.png" align="right" /></a>
22
# promise
33

4-
This a bare bones [Promises/A+](http://promises-aplus.github.com/promises-spec/) implementation.
4+
This is a simple implementation of Promises. It is a super set of ES6 Promises designed to have readable, performant code and to provide just the extensions that are absolutely necessary for using promises today.
55

6-
It is designed to get the basics spot on correct, so that you can build extended promise implementations on top of it.
6+
For detailed tutorials on its use, see www.promisejs.org
77

88
[![Build Status](https://travis-ci.org/then/promise.png)](https://travis-ci.org/then/promise)
99
[![Dependency Status](https://gemnasium.com/then/promise.png)](https://gemnasium.com/then/promise)
1010
[![NPM version](https://badge.fury.io/js/promise.png)](http://badge.fury.io/js/promise)
1111

1212
## Installation
1313

14-
**Server:**
14+
**Server:**
1515

1616
$ npm install promise
17-
18-
**Client:**
19-
20-
You can use browserify on the client, or download a standalone version from [www.promisejs.org](http://www.promisejs.org/implementations/#i-promise)
21-
17+
18+
**Client:**
19+
20+
You can use browserify on the client, or use the pre-compiled script that acts as a pollyfill.
21+
2222
```html
23-
<script src="http://www.promisejs.org/implementations/promise/promise-3.2.0.js"></script>
23+
<script src="https://www.promisejs.org/polyfills/promise-4.0.0.js"></script>
2424
```
2525

2626
## Usage
2727

28-
The example below shows how you can load the promise library (in a way that works on both client and server). It then demonstrates creating a promise from scratch. You simply call `new Promise(fn)`. There is a complete specification for what is returned by this method in [Promises/A+](http://promises-aplus.github.com/promises-spec/).
28+
The example below shows how you can load the promise library (in a way that works on both client and server). It then demonstrates creating a promise from scratch. You simply call `new Promise(fn)`. There is a complete specification for what is returned by this method in [Promises/A+](http://promises-aplus.github.com/promises-spec/).
2929

3030
```javascript
3131
var Promise = require('promise');
@@ -57,11 +57,13 @@ This creates and returns a new promise. `resolver` must be a function. The `re
5757

5858
These methods are invoked by calling `Promise.methodName`.
5959

60-
#### Promise.from(value)
60+
#### Promise.resolve(value)
61+
62+
(deprecated aliases: `Promise.from(value)`, `Promise.cast(value)`)
6163

6264
Converts values and foreign promises into Promises/A+ promises. If you pass it a value then it returns a Promise for that value. If you pass it something that is close to a promise (such as a jQuery attempt at a promise) it returns a Promise that takes on the state of `value` (rejected or fulfilled).
6365

64-
#### Promise.all(array) / Promise.all(a, b, c, ...)
66+
#### Promise.all(array)
6567

6668
Returns a promise for an array. If it is called with a single argument that `Array.isArray` then this returns a promise for a copy of that array with any promises replaced by their fulfilled values. Otherwise it returns a promise for an array that conatins its arguments, except with promises replaced by their resolution values. e.g.
6769

@@ -83,6 +85,8 @@ Promise.all(Promise.from('a'), 'b', Promise.from('c'))
8385

8486
#### Promise.denodeify(fn)
8587

88+
_Non Standard_
89+
8690
Takes a function which accepts a node style callback and returns a new function that returns a promise instead.
8791

8892
e.g.
@@ -101,6 +105,8 @@ var p = read('foo.json', 'utf8')
101105

102106
#### Promise.nodeify(fn)
103107

108+
_Non Standard_
109+
104110
The twin to `denodeify` is useful when you want to export an API that can be used by people who haven't learnt about the brilliance of promises yet.
105111

106112
```javascript
@@ -128,10 +134,14 @@ The call to `.then` also returns a promise. If the handler that is called retur
128134

129135
#### Promise#done(onFulfilled, onRejected)
130136

137+
_Non Standard_
138+
131139
The same semantics as `.then` except that it does not return a promise and any exceptions are re-thrown so that they can be logged (crashing the application in non-browser environments)
132140

133141
#### Promise#nodeify(callback)
134142

143+
_Non Standard_
144+
135145
If `callback` is `null` or `undefined` it just returns `this`. If `callback` is a function it is called with rejection reason as the first argument and result as the second argument (as per the node.js convention).
136146

137147
This lets you write API functions that look like:

index.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var UNDEFINED = new ValuePromise(undefined)
3232
var ZERO = new ValuePromise(0)
3333
var EMPTYSTRING = new ValuePromise('')
3434

35-
Promise.from = Promise.cast = function (value) {
35+
Promise.resolve = function (value) {
3636
if (value instanceof Promise) return value
3737

3838
if (value === null) return NULL
@@ -57,6 +57,14 @@ Promise.from = Promise.cast = function (value) {
5757

5858
return new ValuePromise(value)
5959
}
60+
61+
Promise.from = Promise.cast = function (value) {
62+
var err = new Error('Promise.from and Promise.cast are deprecated, use Promise.resolve instead')
63+
err.name = 'Warning'
64+
console.warn(err.stack)
65+
return Promise.resolve(value)
66+
}
67+
6068
Promise.denodeify = function (fn, argumentCount) {
6169
argumentCount = argumentCount || Infinity
6270
return function () {
@@ -93,7 +101,14 @@ Promise.nodeify = function (fn) {
93101
}
94102

95103
Promise.all = function () {
96-
var args = Array.prototype.slice.call(arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments)
104+
var calledWithArray = arguments.length === 1 && Array.isArray(arguments[0])
105+
var args = Array.prototype.slice.call(calledWithArray ? arguments[0] : arguments)
106+
107+
if (!calledWithArray) {
108+
var err = new Error('Promise.all should be called with a single array, calling it with multiple arguments is deprecated')
109+
err.name = 'Warning'
110+
console.warn(err.stack)
111+
}
97112

98113
return new Promise(function (resolve, reject) {
99114
if (args.length === 0) return resolve([])
@@ -121,6 +136,20 @@ Promise.all = function () {
121136
})
122137
}
123138

139+
Promise.reject = function (value) {
140+
return new Promise(function (resolve, reject) {
141+
reject(value);
142+
});
143+
}
144+
145+
Promise.race = function (values) {
146+
return new Promise(function (resolve, reject) {
147+
values.forEach(function(value){
148+
Promise.resolve(value).then(resolve, reject);
149+
})
150+
});
151+
}
152+
124153
/* Prototype Methods */
125154

126155
Promise.prototype.done = function (onFulfilled, onRejected) {
@@ -149,25 +178,3 @@ Promise.prototype.nodeify = function (callback) {
149178
Promise.prototype['catch'] = function (onRejected) {
150179
return this.then(null, onRejected);
151180
}
152-
153-
154-
Promise.resolve = function (value) {
155-
return new Promise(function (resolve) {
156-
resolve(value);
157-
});
158-
}
159-
160-
Promise.reject = function (value) {
161-
return new Promise(function (resolve, reject) {
162-
reject(value);
163-
});
164-
}
165-
166-
Promise.race = function (values) {
167-
return new Promise(function (resolve, reject) {
168-
values.map(function(value){
169-
Promise.cast(value).then(resolve, reject);
170-
})
171-
});
172-
}
173-

0 commit comments

Comments
 (0)