Callback binding in a library function.

I've recently ran into an issue. I was working on a library and there's a function that accepts a callback;

const data = {}
// need to bind callback to data

I need to bind the callback to an object. But here's the thing, since this is a library, the callback will be provided by the consumer of the library; callbacks in the form of arrow functions cannot be bound. I've tried converting the arrow function to a regular one but then I had to use eval() which causes a reference error if the function contains any global/outer scope variables.
Is there a workaround for this? I need serious help here; my project is stalled midway because of this. (no pun intended)
Also I cannot supply any additional arguments to the callback as the context, i.e if the callback takes 1 argument I cannot provide it with _self(callback, context)
I just want my function to treat all callbacks as just normal functions.

They can certainly be bound to arguments - you just can't change the receiver. You can do callback.bind(null, data) just fine.

If the design of the library’s API is to pass context via this then consumers of the library will need to know not to use arrow functions. An example of this is cucumber.js step definitions where “the world” is passed to the step via this.

If they are passing an arrow function there is no way to re-bind what its this references.


Either the consumer of the library uses _self(callback.bind(data)), or the library author (implementer of _self) invokes, …). Both of these forms work.

Then it's on the consumer of the library not to pass an arrow function if they need to access the receiver (this value). There's nothing your library can (or should) do about this; apart from maybe reminding its users about this in the documentation.

Don't. This doesn't work properly. And if it did, it would mess with everyone's expectations about what value the this keyword will refer to.

Arrow functions are normal functions. You can call them, .call() them, .bind() them like any other function. That the function cannot refer to the this value it is passed doesn't matter to the caller, it's just the same as a function that simply doesn't use the this keyword.

Why not? Should be as simple as _self(arg => callback(arg, data)).