I am trying to refactor jQuery promises with native promise for the below codes.
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;
}
As the above methods involved passing deferred objects to multiple functions and array of deferred objects, just seeking help of any better method to rewrite the above with native promise like 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;
}
Thanks in advance.