Currently, I am in the process of refactoring jQuery promises using native promises for the following code snippets.
public functionA(){
const dArray = Array<JQueryDeferred<{a:string}>>=[];
//other lines of logic
functionB(dArray, /*other parameters*/);
}
private functionB(dArray : Array<JQueryDeferred<{a:string}>>[], /*other arguments*/){
//other lines of logic
for(var i=0;i<10;i++){
dArray.push($.Deferred());
var ele = dArray.length-1;
functionC(dArray[ele], /*other parameters*/)
.done((result: { a:string}) => {
// logic
})
.always() => {
// some additional logic
});
}
}
private functionC(d: JQueryDeferred<{a:string}>):JQueryDeferred<{a:string}>{
if(){//some condition
// some other logic
d.resolve({ a: "completed" });
}
return d;
}
The current methods involve passing deferred objects to multiple functions and an array of deferred objects. I am looking for assistance in finding a better way to rewrite the code using native promises as shown below;
public functionA(){
const pArray = Array<Promise<{a:string}>>=[];
//other lines of logic
functionB(pArray, /*other parameters*/);
}
private functionB(pArray : Array<Promise<{a:string}>>[], /*other arguments*/){
//other lines of logic
for(var i=0;i<10;i++){
pArray.push((new Promise<{ a:string; }>(resolve => resolvePromise = resolve)););
var ele = pArray.length-1;
functionC(pArray[ele], /*other parameters*/)
.then((result: { a:string }) => {
// logic
})
.finally() => {
// some additional logic
});
}
}
private functionC(p: Promise<{a:string}>):Promise<{a:string}>{
if(){//some condition
// some other logic
// i am stuck here..
p.resolve({ a: "completed"}) //no such resolve method to call
// tried with Promise.resolve({ a: "completed"}),
// but my question - will it resolve same way as the index based
// resolve like the jQuery deferred version?
}
return p;
}
Thank you for any help provided.