- 🎨 Rewritten the code from scratch using ES6.
- 💥 Breaking: the
done
method was renamed into call
- 💥 Breaking: the
callback
method was renamed into done
- 💥 Breaking: the
isWaiting
field was renamed into waiting
- 💥 Breaking: the
isDone
field was renamed into is_done
- 🔥 Deprecated
add
- 🔥 Deprecated
wait
- 💡 Added the
check
method which returns true
when the async function should not be called again (so, you will call this method using if (cb.check(fn)) { return; }
). This makes the wait
functionality obsolete.
New example
// Dependencies
const CallbackBuffering = require("cb-buffer");
// Create a new callback buffer
var cb = new CallbackBuffering();
// Callbacks a random unique number after 1 sec
function getUniqueRandomNumberAsync(callback) {
if (cb.check(callback)) { return; }
setTimeout(() => {
debugger
cb.done(Math.random());
}, 1000);
}
// Request the unique number few times.
// It should be unique, and generated once
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
getUniqueRandomNumberAsync(console.log);
// ... after one second
// => 0.3639475910458714
// => 0.3639475910458714
// => 0.3639475910458714
// => 0.3639475910458714
// After one second (after the random number is found)
// we request it again
setTimeout(() => {
getUniqueRandomNumberAsync(console.log);
// => 0.3639475910458714
}, 1000);