Issue with Native promise resolve outside the function

I am trying to refactor jQuery promises with native promise for the below codes.

public functionA(){
    const dArray = Array<JQueryDeferred<{a:string, b:number, doneFuncton : Function}>>=[];
    //other lines of logic
    functionB(dArray, /*other parameters*/);

}

private functionB(dArray : Array<JQueryDeferred<{a:string, doneFuncton : Function}>>[], /*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, doneFuncton : Function }) => {
                // logic
            })
            .always((result: { a:string, doneFuncton : Function }) => {
                // logic
        });
    }
}

private functionC(d: JQueryDeferred<{a:string, doneFuncton : Function}>):JQueryDeferred<{a:string}>{
        if(){//some condition

           // some other logic to return done callback
           d.resolve({ a: "completed", doneFuncton : doneFuncton });

        }
    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, b:number, doneFuncton : Function}>>=[];
    //other lines of logic
    functionB(pArray, /*other parameters*/);

}

private functionB(pArray : Array<Promise<{a:string, doneFuncton : Function}>>[], /*other arguments*/){
    //other lines of logic
    for(var i=0;i<10;i++){
        pArray.push((new Promise<{ a:string; doneFunction:Function }>(resolve => resolvePromise = resolve)););
        var ele = pArray.length-1;

        functionC(pArray[ele], /*other parameters*/)
            .then((result: { a:string, doneFuncton : Function }) => {
                // logic
            })
            .finally((result: { a:string, doneFuncton : Function }) => {
                // logic
        });
    }
}

private functionC(p: Promise<{a:string, doneFuncton : Function}>):Promise<{a:string}>{
        if(){//some condition
           // some other logic to return done callback
           // i am stuck here..
           p.resolve({ a: "completed", doneFuncton : doneFuncton }) //no such resolve method to call
           // tried with Promise.resolve({ a: "completed", doneFuncton : doneFuncton }), 
           // but my question - will it resolve same way as the index based 
           // resolve like the jQuery deferred version?

        }
    return p;
}

Thanks in advance.



Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)