Promise

Simple implementation of JavaScript promises

Members

Aliases

V
alias V = typeof(BlackBox.tupleof)
Undocumented in source.

Functions

except
Promise!(S, F) except(S delegate(F) onCatch)

Registers catch callback, equivalent to then(null, ErrorCallback)

finish
Promise!(S, E) finish(S delegate(V) onResolve)

Registers callback to be called at end. Equivalent to then(onFinally, onFinally)

reject
void reject(E err)

Resolves promise and calles then onError callback

resolve
void resolve()

Resolves promise and calles then onResolve callbacks

resolve
void resolve(T val)

Resolves promise and calles then onResolve callbacks

then
Promise!(S, F) then(S delegate(V) onResolve, S delegate(E) onReject)

Registers on resolve functions (set null for no callback)

Examples

HTTPRequest prom = new Promise!(string, HTTPStatusException)();

prom.then(delegate void(string s) {
    writeln(s);
}).then(null, delegate void(HTTPStatusException e) {
    writeln("Error ", e.status, ": ",  e.msg);
}).except(delegate void(HTTPStatusException e) {
    writeln(e.msg);
}).finish(delegate void() {
    writeln("Finished after error");
});

prom.resolve("My data");
prom.refresh();
prom.reject(new HTTPStatusException(451, "Reject message"));

Meta