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'.
getUserPreferencesng.IPromise <string|void>. Beware if you're catching exception here the promise will be always resolved as successful.