Monday 28 May 2018

Make function synchronous

Hi, I have been working on a simple cube timer app using node. The problem is that when i run best(), Math.min returns infinity because it runs before the execution of sumOfSeconds ends and therefore i get an empty array. Is there a way to solve this problem? Thanks in advance!var sumOfSolves = []; //crete an array of total seconds in each time - used for Ao5, Ao12, best and worst function sumOfSeconds(){ Time.find({}, function(err, data){ if(err) throw err; sumOfSolves = []; for(var i = 0; i < data.length; i++){ sum = data[i].min * 60 + data[i].sec; sumOfSolves.push(sum); } }) } function best(){ sumOfSeconds(); //ERROR function is async that is why it doesnt work var best = Math.min(...sumOfSolves); var position = sumOfSolves.indexOf(best); Time.find({}, function(err, data){ if(err) throw err; var bestTime = data[position]; console.log(sumOfSolves) }) } best();

Submitted May 28, 2018 at 02:40PM by everek123

No comments:

Post a Comment