"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