Jan Willem B
Jan Willem B

Reputation: 3806

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() {
        this.resolve();
    }
}

Upvotes: 1

Views: 2445

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164287

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>) {
        this.resolveFunction(value);
    }

    public reject(reason?: any) {
        this.rejectFunction(reason);
    }

    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:
future.resolve("A_VALUE");
// or reject it:
future.reject("MESSAGE");

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[]> {
        ISSUE_HTTP_REQUEST(url)
            .then(this.future.resolve.bind(this.future))
            .catch(this.future.reject.bind(this.future));

        return this.future.asPromise();
    }
}

Upvotes: 9

Related Questions