Skip to content

Commit e9ede6c

Browse files
committed
Documentation tweaks.
1 parent 9f20077 commit e9ede6c

File tree

3 files changed

+41
-40
lines changed

3 files changed

+41
-40
lines changed

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,39 @@ Let your library support any ES 2015 (ES6) compatible `Promise` and leave the ch
66

77
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.
88

9-
### Library Usage
9+
#### Usage:
1010

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`:
1222

1323
```javascript
14-
var Promise = require('any-promise');
24+
// top of application index.js or other entry point
25+
require('any-promise/register/bluebird')
1526

16-
return Promise
17-
.all([xf, f, init, coll])
18-
.then(fn);
27+
// -or- Equivalent to above, but allows customization of Promise library
28+
require('any-promise/register')('bluebird', {Promise: require('bluebird')})
29+
```
1930

31+
Now that the implementation is registered, you can use any package depending on `any-promise`:
2032

21-
return new Promise(function(resolve, reject){
22-
try {
23-
resolve(item);
24-
} catch(e){
25-
reject(e);
26-
}
27-
});
2833

34+
```javascript
35+
var fsp = require('mz/fs') // mz/fs will use registered bluebird promises
36+
var Promise = require('any-promise') // the registered bluebird promise
2937
```
3038

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.
3242

3343
### Optional Application Registration
3444

@@ -86,41 +96,32 @@ Your preference will be registered globally, allowing a single registration even
8696
require('../register')('es6-promise', {Promise: require('es6-promise').Promise, global: false})
8797
```
8898

89-
#### Example:
90-
91-
Assuming `bluebird` is the desired Promise implementation:
99+
### Library Usage
92100

93-
```bash
94-
# Install preferred promise library
95-
$ npm install bluebird
96-
# Install any-promise to allow registration
97-
$ npm install any-promise
98-
# Install any libraries you would like to use depending on any-promise
99-
$ npm install mz
100-
```
101-
Register your preference in the application entry point before any other `require` of packages that load `any-promise`:
101+
To use any `Promise` constructor, simply require it:
102102

103103
```javascript
104-
// top of application index.js or other entry point
105-
require('any-promise/register')('bluebird')
104+
var Promise = require('any-promise');
106105

107-
// -or- Equivalent to above in Node.js, but browser version needs polyfill or explicit registration
108-
require('any-promise/register')('bluebird', {Promise: require('bluebird')})
109-
```
106+
return Promise
107+
.all([xf, f, init, coll])
108+
.then(fn);
110109

111-
Now that the implementation is registered, you can use any package depending on `any-promise`:
112110

111+
return new Promise(function(resolve, reject){
112+
try {
113+
resolve(item);
114+
} catch(e){
115+
reject(e);
116+
}
117+
});
113118

114-
```javascript
115-
var fsp = require('mz/fs') // mz/fs will use registered bluebird promises
116-
var Promise = require('any-promise') // the registered bluebird promise
117119
```
118120

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.
120122

121-
Again, registration is not required. It should only be called by the application user if overriding the default implementation is desired.
122123

123-
### Advanced Library Usage
124+
#### Advanced Library Usage
124125

125126
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.
126127

register-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = require('./loader')(window, loadImplementation)
99
function loadImplementation(){
1010
if(typeof window.Promise === 'undefined'){
1111
throw new Error("any-promise browser requires a polyfill or explicit registration"+
12-
" e.g: require('any-promise/register')('bluebird', {Promise: require('bluebird')})")
12+
" e.g: require('any-promise/register/bluebird')")
1313
}
1414
return {
1515
Promise: window.Promise,

register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function loadImplementation(implementation){
3737
throw new Error('Cannot find any-promise implementation nor'+
3838
' global.Promise. You must install polyfill or call'+
3939
' require("any-promise/register") with your preferred'+
40-
' implementation, e.g. require("any-promise/register")("bluebird")'+
40+
' implementation, e.g. require("any-promise/register/bluebird")'+
4141
' on application load prior to any require("any-promise").')
4242
}
4343

0 commit comments

Comments
 (0)