Skip to content

Commit cf1a538

Browse files
committed
Document promises in the README.md.
1 parent d6aef41 commit cf1a538

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,48 @@ try {
147147
}
148148
```
149149
150+
### Promises
151+
152+
As of release 0.4.5 it is possible to create async methods that return promises by setting the asyncOptions property of the java object.
153+
154+
Example:
155+
156+
```javascript
157+
var java = require("java");
158+
java.asyncOptions = {
159+
promiseSuffix: 'Promise',
160+
promisify: require('when/node').lift
161+
};
162+
java.classpath.push("commons-lang3-3.1.jar");
163+
java.classpath.push("commons-io.jar");
164+
165+
java.newInstancePromise("java.util.ArrayList")
166+
.then(function(list) { return list.addPromise("item1"); })
167+
.then(function(list) { return list.addPromise("item2"); })
168+
.catch(function(err) { /* handle error */ });
169+
```
170+
171+
* If you don't need promise-returning methods, simply leave java.asyncOptions unset.
172+
* Sync and standard async methods are still generated as in previous releases. In the future we may provide the option to disable generation of standard async methods.
173+
* You are free to choose whatever non-empty suffix you want for the promise-returning methods, but you must specify a value.
174+
* asyncOptions.promisify must be a function that given a node.js style async function as input returns a function that returns a promise that is resolved (or rejected) when the async function has completed. Several Promises libraries provide such functions. This *should* just work, but at the moment one prominent promises library doesn't.
175+
* Note that it should be possible to mix use of two different Promises/A+ conforming libraries. You may be able to use one library for installing the asyncOptions.promisify function, and then use another library everywhere else in your application.
176+
177+
#### Tested Promises Libraries
178+
179+
##### [when](https://www.npmjs.com/package/when)
180+
We use this package in our unit tests, and it passes under all 9 cases of our [test matrix](https://travis-ci.org/joeferner/node-java).
181+
182+
`promisify: require('when/node').lift`
183+
184+
##### [bluebird](https://www.npmjs.com/package/bluebird)
185+
Does not work with node 0.8, but works with node 0.10 and 0.11.
186+
187+
`promisify: require('bluebird').promisify`
188+
189+
##### [Q](https://www.npmjs.com/package/q)
190+
Unfortunately, the popular Q promises library currently does **NOT** work.
191+
150192
# Release Notes
151193
152194
### v0.2.0

0 commit comments

Comments
 (0)