I'm working on a web-based visualization tool of algorithms using javascript, so it needs some pause during the execution. To pause execution I used const delay = ms => new Promise(res => setTimeout(res, ms));
and called function await delay(500);
, which I found on stack overflow, it worked fine with a non-recursive algorithm. And to make recursive algorithm sequential I used return new Promise((resolve,reject)
and .then()
. but now I can't use await inside of return new Promise
, what is the reason, and am I on right track?
my code looks like this(quicksort)
const quick = async(low, high) =>
{
return new Promise((resolve,reject) =>
{
if (low < high)
{
var pivot = color[high]
var i = low -1
for(var j = low; j <= high - 1; j++)
{
// I want some delay here
if(color[j] < pivot)
{
i++
swap(i,j)
// delay here
}
}
var pi = i+1;
swap(pi,high)
// and delay here
quick( low, pi - 1).then(quick( pi + 1, high));
}
resolve();
});
}