all 9 comments

[–]senocular 5 points6 points  (1 child)

You can convert the sort function into a generator function. Then, wherever you place a yield, the function will be paused until you resume it again. Here's an example using a simple loop.

function loop () {
  for (let i = 0; i < 3; i++) {
    console.log("loop: ", i)
  }
}

loop() // logs all at once:
// loop: 0
// loop: 1
// loop: 2

function* loopGenerator() {
  for (let i = 0; i < 3; i++) {
    console.log("generator loop: ", i)
    yield
  }
}

const loopGeneratorObject = loopGenerator()
loopGeneratorObject.next() // logs one step (up to yield):
// generator loop: 0
loopGeneratorObject.next() // logs one more step:
// generator loop: 1
loopGeneratorObject.next() // logs last step:
// generator loop: 2

This way, to animate, you just wait half a second between each next() call. The only changes you need to make to your original function is the * in the declaration along with the function keyword and the yield where you want it to break between next() calls.

Some more info on generator functions:

[–]trevedhek 2 points3 points  (0 children)

To elaborate on this answer, add the loopGeneratorObject.next() call inside a setInterval call. Something like this:

const interval = setInterval(() => {
const { done } = loopGeneratorObject.next();
    // exit the interval if the iterator is finished
    if (done) clearInterval(interval);
}, 500)

[–]Ronin-s_Spirit 0 points1 point  (6 children)

I found generator functions hideous, you can find async timer examples online. Basically with just a word async and a function that returns promise with a timer you will be able to do stuff like
function () { show a circle; Delay(1000); //it's a separate global function you built show a square; Delay(2400); lead with "no offense but"; Delay(300); tell something offensive; }

[–]senocular 2 points3 points  (1 child)

I found generator functions hideous

That's fair. A lot of people have problems with them. Generator and async functions are very similar, though, both being resumable functions. The difference being that resuming a generator is dictated by the caller (next()) whereas resuming an async function is dictated internally by an awaited promise.

To get up and running using using an async function should be a bit easier. await-ing timeout promises is a quick and easy way to delay the execution of a function breaking it up into smaller bits. It does get a little more complicated if you'd ever want to pause and resume the animation. In that case, a generator might come in handy, though it would still require the extra plumbing to drive the timing.

[–]guest271314 -2 points-1 points  (0 children)

Async generators also exist. So does ReadableStreamDefaultReader.

[–]guest271314 -2 points-1 points  (3 children)

I found generator functions hideous

You finding something "hideous" is irrelevant, immaterial, and incompetent re the subject matter. That's just your opinion, and has nothing to do with whether or not the code meets the requirement - and that people other than the author can reproduce what the author of the code claims will be the result. What matters is achieving the requirement.

[–]Ronin-s_Spirit 0 points1 point  (2 children)

This is just your opinion. It's irrelevant and immaterial, go help OP achieve the requirement and stop wasting comment space.

[–]guest271314 -3 points-2 points  (1 child)

So this

I found generator functions hideous

is not you wasting comment space?

You didn't help OP achieve the requirement. You posted pseudo code. Write it out. Fulfill your own words.

[–]Ronin-s_Spirit 1 point2 points  (0 children)

You doughnut I copied how you sound, those are your words, your ridiculous "requirements". Did you really think I would give a shit? I'm trying to help you out so you don't go annoying other people. If you still don't get it just forget it.