1

I'm trying to return a generic promise in the following way

public getUserPreferences: () => ng.IPromise <string> = () => {
        var promise = this.makeRequest<string>('http://someurl.com',null)
            .then((response:string) => {
                    let res: string = response;
                    return res;
                }
            )
            .catch((error) => {
            });

        return promise;
    };

        public makeRequest<T>(וrl: string, data?: any,
                          config?: any, verb?: HttpMethod): ng.IPromise<T> {
        // Cache key contains both request url and data
        var cacheKey = url + '*' + JSON.stringify(data);

        var deferred = this.$q.defer();
        var httpRequest: any;
        var responseData: T;
        var start = new Date().getTime();

        // Trying to retrieve cache data if needed
        if (!config || config.cache != false) {
            responseData = this.cacheService.get(cacheKey);
        }

        if (responseData) {
            deferred.resolve(responseData);
        }
        else {
            switch (verb) {
                case HttpMethod.GET:
                    httpRequest = this.$http.get(url, config);
                    break;
                case HttpMethod.POST:
                    httpRequest = this.$http.post(url, data, config);
                    break;
                case HttpMethod.PATCH:
                    httpRequest = this.$http.patch(url, data, config);
                    break;
                default:
                    httpRequest = this.$http.post(url, data, config);
                    break;

            }

            httpRequest
                .then((res: any) => {
                    responseData = res.data;
                    this.cacheService.put(cacheKey, responseData);
                    deferred.resolve(responseData);
                })
                .catch((error: any) => {
                    deferred.reject(error);
                });
        }

        return deferred.promise;
    }

But I'm getting the following error on getUserPreferences:

Error:(132, 9) TS2322: Type '() => IPromise' is not assignable to type '() => IPromise'. Type 'IPromise' is not assignable to type 'IPromise'. Type 'void' is not assignable to type 'string'.

3
  • remove catch block from getUserPreferences Commented Sep 4, 2016 at 13:16
  • @AlekseyL. thanks! it works! But how can I catch the exception? Commented Sep 5, 2016 at 7:59
  • If this is right place to handle exception and you don't want to return anything in case of exception - you should define return type as ng.IPromise <string|void>. Beware if you're catching exception here the promise will be always resolved as successful. Commented Sep 6, 2016 at 5:32

1 Answer 1

1

Looks like the problem is that you never resolve promise with responseData: no resolve(responseData) meant that promise was resolving implicitly with undefined.

Try this:

public makeRequest<T>(url:string, data?: any): ng.IPromise<T> {
    return this.$http.post(url, data)
           .then((res: any) => {
               return res.data;
           });
}

I also removed redundant deferred object, which you don't need because you can directly return promise.

Another difference. I dropped catch block because it's better to have it at the end of the promise chain (in getUserPreferences).

Sign up to request clarification or add additional context in comments.

1 Comment

My real makeRequest method is more complicated because first I'm trying to retrieve the value from cache if it doesn't exist on cache I want to add it to cache and only then send the response, I will update my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.