0

In JavaScript one can cause code to be executed immediately after the current bit of code has finished executing a la setImmediate or setTimeout(..., 0).

What is the correct term for referring to the gap in execution?

I want to know because I am writing a utility for unit testing that allows a test to continue once one of these gaps has occurred and I want to give it a meaningful name.

At present my code looks like this and the function is just called wait:

export default function wait(done, ...callbacks) {
  let callbackIndex = 0;
  function invokeNext() {
    if (typeof callbacks[callbackIndex] === 'function') {
      setImmediate(() => {
        try {
          callbacks[callbackIndex]();
        }
        catch (e) {
          return done(e);
        }
        callbackIndex += 1;
        invokeNext();
      });
    } else {
      return done();
    }
  }
  invokeNext();
}

It is used like this:

wait(done, () => {
  //... assert something after first code gap
}, 
() => {
  //... assert something after second code gap
});
Jack Allan
  • 14,554
  • 11
  • 45
  • 57
  • To take an operating system analogy, I would say that you _yield_ control to the kernel (here: the browser). But because **yield** is now a Javascript keyword, it's probably not such a good idea... – Arnauld Jul 24 '16 at 15:54
  • can you try conjunction with setTimeout i.e ( setTimeout (500, callbacks[callbackIndex]()); – Indra Uprade Jul 24 '16 at 16:01

1 Answers1

0

You could describe the "gap" as a "task queue check", "task check" or "queue check", see Efficient Script Yielding

  1. Queue the task task.
guest271314
  • 1
  • 15
  • 104
  • 177