A waitable object for Javascript based on ECMAScript 6 Promise.
Your environment must support ECMAScript 6 Promise
class. If not available, it must be shimmed with a third-party implementation such as bulebird
.
const Promise = require("bulebird");
// On web browsers.
window.Promsie = Promise;
// On non-web browsers.
global.Promise = Promise;
npm install kaphein-js-signal
CommonJS style
const { Signal } = require("kaphein-js-signal");
ECMAScript module style
import { Signal } from "kaphein-js-signal";
(async () =>
{
try
{
// Throws SingalTimeoutError after 2000ms.
const signal = new Signal(2000);
// Waits for a signal.
const result = await signal;
// Calls signal.emit({ foo : "foo" }) somewhere.
// Outputs { foo : "foo" }
console.debug(result);
}
catch(error)
{
// Throws an instance of SingalTimeoutError on timeout.
}
})();