A promise for a value T or error E.

Attempts to implement the Promises/A+ spec (, with the following deviations:

- Sections, Due to D strong typing, the only applicable interpretation of "not a function" is null.

- Section 2.2.5: JavaScript-specific, and does not apply to D.

- Section In D, thrown objects may only be descendants of Throwable. By default, Exception objects are caught, and passed to onRejected handlers.

- Section In the case when onFulfilled is null but onRejected is not, the returned promise may be resolved with either the fulfilled value of the current promise or the return value of onRejected. In this case, the type of the returned promise value is the D common type of the two, or void if none.

- Section 2.3.1: Instead of rejecting the promise with a TypeError, an assertion failure is thrown.

- Section 2.3.3: Not implemented. This section facilitates interoperability with other implementations of JavaScript promises, though it could be implemented in D using DbI to support arbitrary then-able objects.

Additionally, this implementation differs from typical JavaScript implementations as follows:

- T may be void. In this case, fulfill, and the delegate in first argument of then, take zero arguments instead of one.

- Instead of the constructor accepting a function which accepts the fulfill / reject functions, these functions are available as regular methods.

- Attempts to fulfill or reject a non-pending promise cause an assertion failure instead of being silently ignored. (The Promises/A+ standard touches on this in section

- catch is called except (because the former is a reserved D keyword).

- finally is called finish (because the former is a reserved D keyword).

- In debug builds, resolved Promise instances check on destruction that their value / error was passed on to a handler (unless they have been successfully fulfilled to a void value). Such leaks are reported to the standard error stream.


Undocumented in source.



alias ValueTuple = A

A tuple of this Promise's value. Either (T) or an empty tuple.


typeof(this) dmd21804workaround()

Work-around for DMD bug 21804:

If your then callback argument is a tuple, insert this call before the then call. (Needs to be done only once per Promise!T instance.)

Promise!(R, F) except(R delegate(E) onRejected)

Registers a rejection handler. Equivalent to then(null, onRejected). Similar to the catch method in JavaScript promises.

Promise!(R, F) finish(R delegate() onResolved)

Registers a finalization handler, which is called when the promise is resolved (either fulfilled or rejected). Roughly equivalent to then(value => onResolved(), error => onResolved()). Similar to the finally method in JavaScript promises.

void fulfill(A value)

Fulfill this promise, with the given value (if applicable).

void reject(E e)

Reject this promise, with the given exception.

Promise!(Unpromise!R, F) then(R delegate(A) onFulfilled, R delegate(E) onRejected)

Registers the specified fulfillment and rejection handlers. If the promise is already resolved, they are called as soon as possible (but not immediately).

Promise!(CommonType!(Unpromise!R, T), F) then(typeof(null) onFulfilled, R delegate(E) onRejected)

Special overload of then with no onFulfilled function. In this scenario, onRejected can act as a filter, converting errors into values for the next promise in the chain.