I am working on a project where im using breezejs to talk to my webapi. I have setup a simple register form with which i can register a new user:
public addUser(user: IApiUser): void {
this.dataContext.entityManager.addEntity(this.dataContext.entityManager.createEntity("ApiUserEntity", user));
}
To write the added entity the the webapi i am calling the saveChanges() method on my entitymanager in breezejs:
// Save any changes for this datacontext
return this.dataContext.entityManager.saveChanges().then((saveResult: breeze.SaveResult): breeze.SaveResult => {
return saveResult;
});
I have downloaded the typescript definition file for breezejs from here , but looking at the saveChanges signature:
saveChanges(entities?: Entity[], saveOptions?: SaveOptions, callback?: SaveChangesSuccessCallback, errorCallback?: SaveChangesErrorCallback): breeze.promises.IPromise<SaveResult>;
It returns a promise of type:
breeze.promises.IPromise<SaveResult>;
Now when i inspect the IPromise in the definition file i get the following interface:
interface IPromise<T> {
then<U>(onFulfill: (value: T) => U, onReject?: (reason: any) => U): IPromise<U>;
then<U>(onFulfill: (value: T) => IPromise<U>, onReject?: (reason: any) => U): IPromise<U>;
then<U>(onFulfill: (value: T) => U, onReject?: (reason: any) => IPromise<U>): IPromise<U>;
then<U>(onFulfill: (value: T) => IPromise<U>, onReject?: (reason: any) => IPromise<U>): IPromise<U>;
catch<U>(onRejected: (reason: any) => U): IPromise<U>;
catch<U>(onRejected: (reason: any) => IPromise<U>): IPromise<U>;
finally(finallyCallback: () => any): IPromise<T>;
}
And i just dont understand why they chose to implement it like this. I am using the Q.js library which has a fail and a .done method etc but if i type these my typescript files wont compile because the interface does not recognize these methods.
I have looked for specific definition files for breeze with Q.js but they dont seem to exist. Offcourse i can just edit the definition file but i would like to know what the reason is for setting it up like this.
fail
is just a synonym for catch
, that works in old browsers. catch
works in all modern browsers. Also EcmaScript 6 have native promises with catch
method.
You can declare missing methods somewhere in your code:
interface IPromise<T> {
fail<U>(onRejected: (reason: any) => U): IPromise<U>;
fail<U>(onRejected: (reason: any) => IPromise<U>): IPromise<U>;
}
This declarations will be merged with declarations from original .d.ts
.