Bear with me, because this takes some setting out.
Consider this trivial tree of values, for the purpose of the question / discussion.
const valuetree = [{
item: 1,
value: 100,
},
{
item: 2,
value: [{
item: 3,
value: [{
item: 4,
value: 200
},
{
item: 5,
value: 200
},
{
item: 6,
value: 200
},
]
}]
},
];
Now if we wanted to write a recursive function to add up all the values, then this in itself is fairly trivial. Like so :
function sumTree(leaf) {
let total = 0;
if (Array.isArray(leaf)) {
leaf.forEach(subLeaf => {
total += sumTree(subLeaf);
});
} else {
if (Array.isArray(leaf.value)) {
total += sumTree(leaf.value);
} else {
total += Number(leaf.value);
}
}
return total;
}
Now suppose we want to use promises to achieve the same thing?
This is what I end up with ...
async function sumTreeAsync(leaf) {
let promises = [];
if (Array.isArray(leaf)) {
leaf.forEach(async subLeaf => {
promises.push(sumTreeAsync(subLeaf));
});
} else {
if (Array.isArray(leaf.value)) {
promises.push(sumTreeAsync(leaf.value));
} else {
promises.push(leaf.value);
}
}
return promises;
}
So what gets returned looks like this
[ Promise { [ 100 ] }, Promise { [ [Promise] ] } ]
Which makes sense, an array of 2 top level items, a promise of a literal value, and a promise that returns a nested array of promises.
So now as some function like Promise.all only deals with a flat array of promises, I would have to recurse the nested promises to arrive at the same outcome. So now I have to recurse the tree twice, which just smells so bad.
So perhaps I need to return resolved promises from the 2 cases where the subleaf is an array?
Is it even worth doing this or should recursion just be synchronous?