Skip to content

Instantly share code, notes, and snippets.

@panta82
Last active June 25, 2018 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panta82/6daca2ddf68fe034d1ba8e78d20a7033 to your computer and use it in GitHub Desktop.
Save panta82/6daca2ddf68fe034d1ba8e78d20a7033 to your computer and use it in GitHub Desktop.
Call order verification problem

"Consume" is a "black-box" function in this format:

function consume(data, callback) {
  //...
}

Caller puts in some data and is called back with the callback. Callbacks are supposed to be in the same sequence as input calls. So if callers A, B, C call consume() in that order, their callbacks should be called in that same order.

Write a function that wraps consume() and throws an error if that order is not honored.

Example:

function createSafeConsume(consume) {
  return function safeConsume(data, callback) {
    consume(data, (...args) => {
      // TODO: Make sure this callback is in correct order
      callback(...args);
    });
  }
}

const safeConsume = createSafeConsume(consume);
safeConsume('a', () => {});
safeConsume('b', () => {});
safeConsume('c', () => {});
// ERROR! b has returned before a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment