https://github.com/nikku/nashorn-async
Simple async support for Nashorn (the Java 8 JavaScript Engine)
https://github.com/nikku/nashorn-async
Last synced: about 1 year ago
JSON representation
Simple async support for Nashorn (the Java 8 JavaScript Engine)
- Host: GitHub
- URL: https://github.com/nikku/nashorn-async
- Owner: nikku
- Created: 2014-12-01T14:56:24.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2023-08-15T15:44:02.000Z (almost 3 years ago)
- Last Synced: 2025-07-08T15:03:58.726Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 13.7 KB
- Stars: 18
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# nashorn-async
[](https://github.com/nikku/nashorn-async/actions/workflows/CI.yml)
Asynchronous scripting support for [Nashorn](http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html).
This project adds optional asynchronous scripting support to javascript snippets evaluated with nashorn.
A snippet may call `var done = async()` to switch to asynchronous mode.
In asynchronous mode the overall script execution will not finish until
all timers and intervals got cleared or elapsed _and_ the `done` callback
got invoked with `(err, result)`.
## Example Code
A simple asynchronous script:
```javascript
var done = async();
var timer = setTimeout(function() {
// do work
done(null, 'WORK DONE!');
}, 100);
```
A timeout may also be cleared:
```javascript
clearTimeout(timer);
done();
```
Scripts may be synchronous, too and simply return a computed value:
```javascript
function doWork() {
return 'WORK DONE!';
}
return doWork();
```
## Async Helpers
Inside a script the following async helpers are available:
* `setTimeout`
* `clearTimeout`
* `setInterval`
* `clearInterval`
## Using the library from Java
Executing scripts with async support from Java:
```java
ScriptRunner execution = new ScriptRunner(script);
try {
Object result = execution.execute();
} catch (Exception e) {
// catches both synchronous and
// asynchronous exceptions
}
```
## Extending the implementation
Simply evaluate the [`event-loop.js`](https://github.com/nikku/nashorn-async/blob/master/src/main/resources/js/event-loop.js) script that adds optional async support in your script prior to evaluating the target script inside a `exec(function() { ...actual script... })` block. See [ScriptRunner](https://github.com/nikku/nashorn-async/blob/master/src/main/java/de/nixis/scripting/ScriptRunner.java) for details.
## Compatible Libraries
Using this library the following projects have been reported to work with Nashorn:
* [pdfmake](http://pdfmake.org)
## LICENSE
MIT