You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-38Lines changed: 39 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,29 +6,39 @@ Let your library support any ES 2015 (ES6) compatible `Promise` and leave the ch
6
6
7
7
If no preference is registered, defaults to the global `Promise` for newer Node.js versions. The browser version defaults to the window `Promise`, so polyfill or register as necessary.
8
8
9
-
###Library Usage
9
+
#### Usage:
10
10
11
-
To use any `Promise` constructor, simply require it:
11
+
Assuming `bluebird` is the desired Promise implementation:
12
+
13
+
```bash
14
+
# Install preferred promise library
15
+
$ npm install bluebird
16
+
# Install any-promise to allow registration
17
+
$ npm install any-promise
18
+
# Install any libraries you would like to use depending on any-promise
19
+
$ npm install mz
20
+
```
21
+
Register your preference in the application entry point before any other `require` of packages that load `any-promise`:
12
22
13
23
```javascript
14
-
varPromise=require('any-promise');
24
+
// top of application index.js or other entry point
25
+
require('any-promise/register/bluebird')
15
26
16
-
returnPromise
17
-
.all([xf, f, init, coll])
18
-
.then(fn);
27
+
// -or- Equivalent to above, but allows customization of Promise library
Now that the implementation is registered, you can use any package depending on `any-promise`:
20
32
21
-
returnnewPromise(function(resolve, reject){
22
-
try {
23
-
resolve(item);
24
-
} catch(e){
25
-
reject(e);
26
-
}
27
-
});
28
33
34
+
```javascript
35
+
var fsp =require('mz/fs') // mz/fs will use registered bluebird promises
36
+
varPromise=require('any-promise') // the registered bluebird promise
29
37
```
30
38
31
-
Libraries using `any-promise` should only use [documented](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) functions as there is no guarantee which implementation will be chosen by the application author. Libraries should never call `register`, only the application user should call if desired.
39
+
It is safe to call `register` multiple times, but it must always be with the same implementation.
40
+
41
+
Again, registration is *optional*. It should only be called by the application user if overriding the global `Promise` implemementation is desired.
32
42
33
43
### Optional Application Registration
34
44
@@ -86,41 +96,32 @@ Your preference will be registered globally, allowing a single registration even
Now that the implementation is registered, you can use any package depending on `any-promise`:
112
110
111
+
returnnewPromise(function(resolve, reject){
112
+
try {
113
+
resolve(item);
114
+
} catch(e){
115
+
reject(e);
116
+
}
117
+
});
113
118
114
-
```javascript
115
-
var fsp =require('mz/fs') // mz/fs will use registered bluebird promises
116
-
varPromise=require('any-promise') // the registered bluebird promise
117
119
```
118
120
119
-
It is safe to call `register` multiple times, but it must always be with the same implementation.
121
+
Libraries using `any-promise` should only use [documented](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) functions as there is no guarantee which implementation will be chosen by the application author. Libraries should never call `register`, only the application user should call if desired.
120
122
121
-
Again, registration is not required. It should only be called by the application user if overriding the default implementation is desired.
122
123
123
-
### Advanced Library Usage
124
+
####Advanced Library Usage
124
125
125
126
If your library needs to branch code based on the registered implementation, you can retrieve it using `var impl = require('any-promise/implementation')`, where `impl` will be the package name (`"bluebird"`, `"when"`, etc.) if registered, `"global.Promise"` if using the global version on Node.js, or `"window.Promise"` if using the browser version. You should always include a default case, as there is no guarantee what package may be registered.
0 commit comments