Skip to content

Commit 50d76ee

Browse files
committed
Update README for enhanced asyncOptions.
1 parent 5de4229 commit 50d76ee

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,18 @@ try {
147147
}
148148
```
149149
150-
### Promises
150+
### AsyncOptions: control over the generation of sync, async & promise method variants.
151151
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.
152+
As of release 0.4.5 it became possible to create async methods that return promises by setting the asyncOptions property of the java object. With release 0.4.7 this feature is extended to allow changing the suffix assigned for sync and async method variants, and to further configure this module to optionally omit generation of any of these variants.
153153
154154
Example:
155155
156156
```javascript
157157
var java = require("java");
158158
java.asyncOptions = {
159-
promiseSuffix: "Promise",
159+
asyncSuffix: undefined, // Don't generate node-style methods taking callbacks
160+
syncSuffix: "" // Sync methods use the base name(!!)
161+
promiseSuffix: "Promise", // Generate methods returning promises, using the suffix Promise.
160162
promisify: require("when/node").lift
161163
};
162164
java.classpath.push("commons-lang3-3.1.jar");
@@ -170,13 +172,18 @@ java.newInstancePromise("java.util.ArrayList")
170172
.catch(function(err) { /* handle error */ });
171173
```
172174
173-
* If you don't want promise-returning methods, simply leave `java.asyncOptions` unset.
174-
* 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.
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.
175+
#### NOTES:
176+
* If you want the defacto standard behavior, simply don't set java.asyncOptions.
177+
* If you do provide asyncOptions, be aware that this module will not generate method variants of a given flavor if you don't provide a string value for the corresponding suffix (`asyncSuffix`, `syncSuffix`, `promiseSuffix`). In the example above, the application is configured to omit the method variants using node-style async callback functions.
178+
* If you provide `asyncOptions.promiseSuffix` then you must also set `asyncOptions.promisify` to a function that *promisifies* a node-style async function. I.e. the provided function must take as input a function whose last argument is a node callback function, and it must return an equivalent promise-returning function. Several Promises/A+ libraries provide such functions, but it may be necessary to provide a wrapper function. See `testHelpers.js` for an example.
179+
* If you provide `asyncOptions.promisify` then you must provide a *non-empty* string for `asyncOptions.promiseSuffix`.
180+
* Either (but not both) `asyncSuffix` or `syncSuffix` can be the empty string. If you want the defacto standard behavior for no suffix on async methods, you must provide an empty string for `asyncSuffix`.
181+
* We've tested promises with five Promises/A+ implementations. See `testHelpers.js` for more information.
178182
* 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.
179183
184+
##### Special note about the exported module functions `newInstance`, `callMethod`, and `callStaticMethod`.
185+
These methods come in both async and sync variants. If you provide the `promisify` and `promiseSuffix` attributes in asyncOptions then you'll also get the Promises/A+ variant for these three functions. However, if you change the defacto
186+
conventions for the `syncSuffix` (i.e. 'Sync') and/or `asyncSuffix` (i.e. '') it will not affect the naming for these three functions. I.e. no matter what you specify in asyncOptions, the async variants are named `newInstance`, `callMethod`, and `callStaticMethod`, and the sync variants are named `newInstanceSync`, `callMethodSync`, and `callStaticMethodSync`.
180187
181188
# Release Notes
182189

0 commit comments

Comments
 (0)