0

I have the following flow of methods

EDIT: where the last methods returns a promise and I want this to propagate back up to the first method in the chain. (I know how to return directly from a promise to a calling function, not sure how it will propagate up the chain?)

function A (input) {
    //do something, input = ...
    B (input)
        .then( (result) => { console.log(result) })
        .catch ( (error) => { console.log(error) });
}

function B (intermediate1) {
    //do something, intermediate1 = ....
    return new Promise ( (resolve, reject) => {
        C(intermedediate1)
            .then( (result1) => {
                //do something to result1
                resolve(result1);
            })
            .catch( (error1) => {
                reject(error1);
            })
   }
}

function C (intermediate2) {
    //do something, intermediate2 = ....
    return new Promise ( (resolve, reject) => {
        D(intermedediate2)
            .then( (result) => {
                //do something to result
                resolve(result);
            })
            .catch( (error) => {
                reject(error);
            })
   }
}

function D (request) {
    return new Promise ( (resolved, reject) => {
        //some ASYNC task with request with result and error returned
        if (error)
            return reject(error);
        resolve(result);
    }
}

Function A does something to input, call B, which does something more, calls C and so on till D which calls an async method and returns a promise.

I am not sure how to propagate this promise back up the flow to C, B and finally A? I want to do something in each of the methods when I recived the promise back from D and so on.

amp
  • 109
  • 1
  • 1
  • 9
  • I know how to return the response/promise directly from an async call to the parent method. I don't know how to do it in such a chain of methods – amp Aug 28 '17 at 11:02
  • I think the one way would be to use a subscribe/publish pattern for your functions. That way you could store all the functions in an array and have complete control over the flow (array.reverse.forEach etc.) While your function D can be an async function with promise await.. – Goran.it Aug 28 '17 at 11:03
  • The chain doesn't change anything, just read the answers in the linked dup, there's your answer. – Teemu Aug 28 '17 at 11:03

1 Answers1

-1

i think you are looking for something like this.

function A (input) {
    //do something, input = ...
    B (input).then(function(){console.log("resolved")});//here B() will return a promise object
    
}

function B (intermediate1) {
    //do something, intermediate1 = ....
    return C(intermediate1);
}

function C (intermediate2) {
    //do something, intermediate2 = ....
    return D(intermediate2);
}

function D (request) {
    return new Promise ( (resolve, reject) => {
        //some ASYNC task with request with result and error returned
        setTimeout(function(){resolve()});
        if (false)
            return reject(error);
        //resolve(result);
    })
}

A()
Nemani
  • 778
  • 5
  • 12
  • yes something like this, but I want to do something with the result of D in C, then pass it back to B, do something, and then finally to A? – amp Aug 28 '17 at 11:13
  • 1
    Oh grace period... An answer 7 minutes after the question is closed. And it doesn't _explain_ how it's supposed to work. – Cerbrus Aug 28 '17 at 11:13
  • @amp: That's more complex than a simple callback. you'll probably want to register some kind of stack of callbacks... – Cerbrus Aug 28 '17 at 11:14
  • @Cerbrus at least i got the question right – Nemani Aug 28 '17 at 11:15
  • common why down vote, this question is wrongly marked duplicate, check his comment on my answer – Nemani Aug 28 '17 at 11:18
  • @AmberNemani: His comment tells you your answer doesn't do what he wants. He wants separate callbacks per "level" that get executed in order D>C>B>A. – Cerbrus Aug 28 '17 at 11:29
  • @Cerbrus correct, but are you sure its duplicate of the other question? – Nemani Aug 28 '17 at 11:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/153019/discussion-between-amber-nemani-and-cerbrus). – Nemani Aug 28 '17 at 11:33
  • I have added what I think might be a solution. Can someone please verify? At least it explains what I want to do. – amp Aug 28 '17 at 11:40
  • @amp: I think you'd be the most qualified person to verify if the code does what -you- want it to do. – Cerbrus Aug 28 '17 at 11:45
  • @amp yeah, that the way to do it if you want to handle the returned object in each of the function – Nemani Aug 28 '17 at 11:46
  • @amp i gave the solution thinking that you wanted to handle the output in the first calling function i.e. `A` and i gave the solution accordingly, i hope it helped to derive your ultimate solution – Nemani Aug 28 '17 at 11:48