I have been attempting to implement a generic promise return in my code:
public getUserData: () => ng.IPromise <string> = () => {
var promise = this.makeRequest<string>('http://someurl.com',null)
.then((response:string) => {
let responseData: string = response;
return responseData;
}
)
.catch((error) => {
});
return promise;
};
public makeRequest<T>(url: string, data?: any,
config?: any, verb?: HttpMethod): ng.IPromise<T> {
// Cache key holds both request URL and data
var cacheKey = url + '*' + JSON.stringify(data);
var deferred = this.$q.defer();
var httpRequest: any;
var responseData: T;
var startTime = new Date().getTime();
// Attempting to retrieve cached data if necessary
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;
}
However, I am encountering the following error message when calling 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'.