Currently, I have the following lines of code that utilize the JQueryDeferred object from the type definition class jquery.d.ts. My goal is to replace jQuery deferred with TypeScript promises.
Here is the existing JQueryDeferred code:
class A {
private b: class B = new B();
public function1(param:string):JQueryDeferred<object> {
var obj: JQueryDeferred<object> = $.Deferred();
b.registerFunction1(obj);
if(param) {//some other condition checking code)
obj.resolve();
}
return obj;
}
}
class B {
public registerFunction1(obj:JQueryDeferred<object>): void {
domhandler.addEventListner(dom, 'onFunction1', onFunction1.bind(obj));
}
public onFunction1(obj:JQueryDeferred<object>, evt:KeyboardEvent):void {
obj.resolve(evt);
}
}
So far, I have refactored the code for class A as follows:
class A {
private b: class B = new B();
public function1(param:string):Promise<object>{
var obj: Promise<object> = new Promise((resolve, reject) => {
b.registerFunction1(obj);
if(param){//some other condition checking code
resolve();
}
});
return obj;
}
}
However, I am unsure how to rewrite the complete code for class B, as I have not encountered promise objects being passed as function arguments before. Even if they were passed, calling resolve() of that promise object in the binder function "onFunction1" is not supported in TypeScript.
Could someone assist me in refactoring the code lines for class B to integrate TypeScript promises?