Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
199
rated 0 times [  200] [ 1]  / answers: 1 / hits: 23043  / 9 Years ago, sun, january 10, 2016, 12:00:00

Language: JavaScript






Recursion - Not my Favorite Topic.



Promises - They can get confusing.



Recursion + Promises - I need to program in a padded room.



I made this little JS Fiddle puzzle I call The RecursiveFunHouse as comedic way of keeping my sanity by simplifying the issue into something silly. Hopefully yall can get a laugh out of my pain :)



The Problem:



Each recursive call is dependent on the outcome of the previous call, but in order to get the outcome I must run an asynchronous task and use that outcome in other sub tasks.



The Recursive Fun House helped me boil down the issue to this - the original recursive loop is continuing on with an undefined value as the sub tasks are still executing.



The Fun House - loops around collecting random numbers between (-99) and 99. Once the last difference between the last number and the new number is positive the fun is over



Printing Made it here 1...Made it here 6 should indicate that the sub-tasks were handled correctly and we have a value for the next loop.



Current it prints 1,2,3,6,4,5 :(



recursiveFunHouse.js



var recursiveFunHouse = function(num){
console.log(Made it here 1);
var newNum = performSideTasks();

console.log(Made it here 6);
console.log(newNum);
console.log(newNum);
if(newNum-num >0 ){
recursiveFunHouse(newNum);
}
else{
console.log(The FunHouse Generated These Numbers :)
for(var i = 0; i <numList.length; i++){
console.log(numList[i]);
}
}

};

var performSideTasks = function(){
console.log(Made it here 2);
someAsyncTask().then(function(num){
anotherTask(num);
console.log(made it here 5);
return num;
});


}

var someAsyncTask = function(){
return new Promise (function(resolve, reject) {
console.log(made it here 3);
var randNum = Math.floor(Math.random()*99) + 1;
randNum *= Math.floor(Math.random()*2) == 1 ? 1 : -1;

setTimeout(function() {
numList.push(randNum)
resolve(randNum)
}, 100);
});
}




var anotherTask = function(num){
console.log(made it here 4);
console.log(num);

};

var numList= [];

recursiveFunHouse(20);


Note - Forgive my pathetic return statement return num; It just shows what I wish I could tell my computer.



Questions About Recursion and Promises:



1) Should I be worried in the first place about going into the next recursive loop with a promise not yet resolved?



2) If not, what is a clean way to keep this function decomposition and force each recursive loop to wait for the resolution of the last call?






Recursion and Promises can seem wizard-level-95 magical sometimes... that's all I'm saying. Question-Done.


More From » recursion

 Answers
12

As stated in the comment, you have to slightly modify performSideTasks to make it return the Promise:



var performSideTasks = function () 
{
console.log( Made it here 2 )
return someAsyncTask().then( function ( num )
{
anotherTask(num);
console.log(made it here 5)
return num
} )
}


Then you can use the asynchronous result in the then() method of the main function.



var recursiveFunHouse = function ( num )
{
console.log( Made it here 1 )
performSideTasks().then( function ( newNum )
{
console.log( Made it here 6 )
console.log( newNum )
console.log( newNum )
if ( newNum-num > 0 )
{
recursiveFunHouse( newNum )
}
else
{
console.log( The FunHouse Generated These Numbers : )
for( var i = 0 ; i <numList.length ; i++ )
{
console.log( numList[i] )
}
}
} )
}

[#63776] Thursday, January 7, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;