How to return a proper Promise with TypeScript
It is considered a good practice to embed the whole function body inside the Promise
constructor, so should any error happen, it would be converted to a rejection. In this case it solves your problem too I believe.
saveMyClass(updatedMyClass: MyClass) {
return new Promise<Package>((resolve, reject) => {
//saving MyClass using http service
//return the saved MyClass or error
var savedPackage : Package = updatedPackage;
if (isSomeCondition) {
throw new Error('No reason but to reject');
}
setTimeout( () => {
resolve(savedPackage);
}, 1500);
});
}