Skip to content

Commit cecac70

Browse files
committed
Merge pull request joeferner#189 from jimlloyd/feature/promises-clarifications.r1
Clarifications to Promises implementation (mostly documentation).
2 parents 23b6f58 + 94f1886 commit cecac70

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

README.md

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,38 +156,27 @@ Example:
156156
```javascript
157157
var java = require("java");
158158
java.asyncOptions = {
159-
promiseSuffix: 'Promise',
160-
promisify: require('when/node').lift
159+
promiseSuffix: "Promise",
160+
promisify: require("when/node").lift
161161
};
162162
java.classpath.push("commons-lang3-3.1.jar");
163163
java.classpath.push("commons-io.jar");
164164

165+
java.import("java.util.ArrayList"); // see NOTE below
166+
165167
java.newInstancePromise("java.util.ArrayList")
166168
.then(function(list) { return list.addPromise("item1"); })
167169
.then(function(list) { return list.addPromise("item2"); })
168170
.catch(function(err) { /* handle error */ });
169171
```
170172
171-
* If you don't need promise-returning methods, simply leave java.asyncOptions unset.
173+
* If you don't want promise-returning methods, simply leave `java.asyncOptions` unset.
172174
* 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`
175+
* `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/A+ libraries provide such functions, but it may be necessary to provide a wrapper function. See `testHelpers.js` for an example.
176+
* You are free to choose whatever non-empty `promiseSuffix` you want for the promise-returning methods, but you must specify a value.
177+
* We've tested with five Promises/A+ implementations. See `testHelpers.js` for more information.
178+
* NOTE: Due to specifics of initialization order, the methods `java.newInstancePromise`, `java.callMethodPromise`, and `java.callStaticMethodPromise` are not available until some other java method is called. You may need to call some other java method such as `java.import()` to finalize java initialization.
188179
189-
##### [Q](https://www.npmjs.com/package/q)
190-
Unfortunately, the popular Q promises library currently does **NOT** work.
191180
192181
# Release Notes
193182

testHelpers.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ java.classpath.push("test/");
66
java.classpath.push("test/commons-lang3-3.1.jar");
77
java.classpath.push("test8/");
88

9+
function promisifyQ(f) {
10+
// Q doesn't provide a promisify function that works directly on a method.
11+
// The .denodeify() (aka .nfbind()) function requires a bound function.
12+
return function(/* arguments */) {
13+
return require('q').nbind(f, this).apply(undefined, arguments);
14+
}
15+
}
16+
917
java.asyncOptions = {
1018
promiseSuffix: 'Promise',
11-
promisify: require('when/node').lift // when works with all three node versions
12-
13-
// PASSES in all three node versions: 0.8.28, 0.10.35, 0.11.14
14-
// promisify: require('when/node').lift // when works with all three node versions
15-
// promisify: require('promise').denodeify // promise works with all three node versions
16-
// promisify: require('vow-node').promisify // vow-node works with all three node versions
17-
18-
// PASSES in Node 0.10, 0.11. (incompatible with Node 0.8).
19-
// promisify: require('bluebird').promisify // bluebird requires node >=0.10
19+
promisify: require('when/node').lift // https://github.com/cujojs/when
2020

21-
// FAILS:
22-
// promisify: require('q').denodeify // FAILS: Q triggers assertion failure in node_object_wrap.h, line 61
23-
// promisify: require('p-promise').denodeify // FAILS: P-promise does not implement catch().
21+
// We've tested with 5 different Promises/A+ implementations:
22+
// promisify: require('bluebird').promisify // https://github.com/petkaantonov/bluebird/
23+
// promisify: require('promise').denodeify // https://github.com/then/promise
24+
// promisify: require('vow-node').promisify // https://github.com/dfilatov/vow-node
25+
// promisify: require('when/node').lift // https://github.com/cujojs/when
26+
// promisify: promisifyQ // https://github.com/kriskowal/q requires wrapper promisifyQ.
2427
};
2528

2629
module.exports.java = java;

0 commit comments

Comments
 (0)