Jan Willem B Jan Willem B - 1 year ago 390
TypeScript Question

Resolve or reject Promise later in TypeScript

In an Angular2/TypeScript project, on showing a dialog, the system returns a Promise object to the caller, which will be resolved after the dialog is closed by the user.

The Promise class interface does not expose resolve() or reject() methods, so I have to save references to these methods to call them later.

This doesn't look right. Is there a better way?

class Dialog {
private resolve;
private reject;

show(): Promise<any> {
var p = new Promise<any>((resolve, reject) => {
//save method references for later use
this.resolve = resolve;
this.reject = reject;
return p;

close() {

Answer Source

I needed something like this, so I created this future class:

class Future<T> implements PromiseLike<T> {
    private promise: Promise<T>;
    private resolveFunction: (value?: T | PromiseLike<T>) => void;
    private rejectFunction: (reason?: any) => void;

    constructor(promise?: Promise<T>) {
        if (!(this instanceof Future)){
            return new Future(promise);

        this.promise = promise || new Promise(this.promiseExecutor.bind(this));

    public asPromise(): Promise<T> {
        return this.promise;

    public then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => TResult | PromiseLike<TResult>): Future<TResult>;
    public then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => void): Future<TResult>;
    public then<TResult>(onfulfilled?: (value: T) => TResult | PromiseLike<TResult>, onrejected?: (reason: any) => any): Future<TResult> {
        return new Future(this.promise.then(onfulfilled, onrejected));

    public catch(onrejected?: (reason: any) => T | PromiseLike<T>): Future<T>;
    public catch(onrejected?: (reason: any) => void): Future<T>;
    public catch(onrejected?: (reason: any) => any): Future<T> {
        return new Future(this.promise.catch(onrejected));

    public resolve(value?: T | PromiseLike<T>) {

    public reject(reason?: any) {

    private promiseExecutor(resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) {
        this.resolveFunction = resolve;
        this.rejectFunction = reject;

Using it like so:

let future = new Future<string>();

// do what not and then:
// or reject it:

You can also save the future instance, return it, and then resolve/reject later:

class MyClass {
    private future: Future<string[]>;

    constructor() {
        this.future = new Future<string[]>();

    fetch(url: string): Promise<string[]> {

        return this.future.asPromise();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download