1

I am new to promises. I have to traverse a tree that returns a promise from its getNode function. I have successfully found the first call to root using the prom.then(....).

However, the next step doesn't work. Specifically the recursive call returns undefined on the line:

 leftTree = inOrderPromise(ptree,leftIndex);

The code correctly calls the function again, and getting the correct node into root, but I can't figure out how to combine my answers. How would I do this?

TYIA

PromiseTree.prototype.getNode = function (index, callback) {
    return Promise.resolve(this.data[index]);
};

var inOrderPromise = function(ptree, rootIndex){
    var prom = ptree.getNode(rootIndex,function(){});
    prom.then(function(root){

        var leftTree = [];
        if(root.left){//if node has a left, recursively traverse
            var leftIndex = root.left;
            leftTree = inOrderPromise(ptree, leftIndex);
        }

        var rightTree = [];
        if(root.right){//if node has a right, recursively traverse
            var rightIndex = root.right;
            rightTree = inOrderPromise(ptree, rightIndex);
        }


        //results: leftTree, root, rightTree
        console.log(leftTree.concat(root.value).concat(rightTree));
        return leftTree.concat(root.value).concat(rightTree);
        });
}
Kishan Oza
  • 1,707
  • 1
  • 16
  • 38
  • 1
    Why are you promisifying a function that is synchronous? Oh, and a hint, if you want a function to return something, it needs to `return` something – Jaromanda X Nov 21 '17 at 07:41
  • 1
    Possible duplicate of [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – ponury-kostek Nov 21 '17 at 13:18

1 Answers1

0

use a generator function, they are used to basically perform async iterations a lot easier, they also work nicely with promises. You may have to tweak the code a bit, i didnt run this but its in the right direction. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators

PromiseTree.prototype.getNode = function (index, callback) {
    return Promise.resolve(this.data[index]);
};

function *inOrderPromise(ptree, rootIndex){
        var root = yield ptree.getNode(rootIndex);
        var leftTree = [];
        if(root.left){
            leftTree = yield *inOrderPromise(ptree, root.left);
        }
        var rightTree = [];
        if(root.right){
            rightTree = yield *inOrderPromise(ptree, root.right);
        }


        //results: leftTree, root, rightTree
        console.log(leftTree.concat(root.value).concat(rightTree));
        return leftTree.concat(root.value).concat(rightTree);
}
Joey
  • 1,075
  • 8
  • 13
  • heres something similar https://stackoverflow.com/questions/32789593/recursive-generators-in-javascript – Joey Nov 21 '17 at 07:00
  • Okay, so I got it to stop giving me compile errors (yay!). Now wen I run it I get: InOrderPromise {[[GeneratorStatus]]: "suspended"} and still no output. – user8973233 Nov 21 '17 at 07:34
  • `rightTree = *inOrderPromise(ptree, rightIndex);` - really? that's syntactically correct now in javascript? – Jaromanda X Nov 21 '17 at 07:42
  • You will want to use `async`/`await` for promises, not `*`/`yield` – Bergi Nov 21 '17 at 16:11
  • @Bergi, no, `* / yield` also known as Generator functions are great for async iterations. – Joey Nov 21 '17 at 16:20
  • @Bergi if you can think of another way to do it using async await, post an answer – Joey Nov 21 '17 at 16:26
  • @Joey They're absolutely not great for *async* iteration - they're fine for synchronous (but lazy) iteration. You just created a generator function that will yield promises - appears pretty useless to me. Can you demonstrate how to call this function? – Bergi Nov 21 '17 at 16:30
  • before you start knocking generators and saying it is usles you should learn how they are used. ie calling one... it seems you are lacking some fundamentals to see how this is useful. take a look at the links i posted above and maybe do some research on async loops. you will soon realize why they are important – Joey Nov 21 '17 at 16:35
  • to be clear when i say async iteration I mean making your async calls in a loop/recursive function act in a synchronous manner. – Joey Nov 21 '17 at 16:47