Table of Contents
Standard Module: promise
To use the promise
module and the Promise
interface in a HipHop program,
import the JavaScript module with:
import { promise, Promise } from "@hop/hiphop/modules/promise.js";
The promise
HipHop module maps JavaScript promises to
HipHop signals. It enables to spawn a JavaScript promise which
upon resolution or rejection emits a HipHop signal.
Promise
The Promise
interface declares the following HipHop signals:
out value
, whether it resolves or rejects, aPromise
HipHop module emits a single signal. That signal is an object with two attributes:{res: resolution-value, rej: rejection-value}
.
promise(duration) implements Promise
A HipHop promise
module is an asynchronous tasks that run in the
background of the application. When it resolves or rejects, it emits
its signal value
. Example:
// emits v with the value { resolve: "yep", reject: false }
run promise(new Promise((res, rej) => res("yep")))) { v as value };
★ Example: promise-mod.hh.js
import * as hh from "@hop/hiphop";
import { promise } from "@hop/hiphop/modules/promise.js";
import { format } from "util";
hiphop module prg(resolve) {
out STATUS;
signal v;
fork {
run promise(new Promise((res, rej) => res(45))) { v as value };
} par {
await (v.now);
emit STATUS(v.nowval.res);
}
pragma { resolve(false); }
}
export const mach = new hh.ReactiveMachine(prg);
mach.outbuf = "";
mach.debug_emitted_func = emitted => {
mach.outbuf += format(emitted) + "\n";
};
mach.batchPromise = new Promise((res, rej) => mach.init(res));
mach.react();
Implementation
- JavaScript implementation: promise.hh.js
- TypeScript type declaration: promise.d.ts