scf4 / callbaxx

[Satire]🔥 JS utility library to bring classic callback style programming to synchronous code — ES6? More like ES Sucks.
MIT License
9 stars 1 forks source link

Inconsistency in API design. #14

Open ndorf opened 5 years ago

ndorf commented 5 years ago

Shouldn't this

var isArray = require('callbaxx').isArray;

be more like

require('callbaxx').isArray(isArray => { /* ... */ });

?

scf4 commented 5 years ago

Hi Nathan, I'm not sure I follow. Could you please elaborate?

ndorf commented 5 years ago

Why should one ever have to type var anything = ... in JavaScript when a perfectly good callback interface can exist instead?

The ideal scenario would of course be `require('callbax', module => { module.isArray(...) }) but since that's built into JS, the least you could do is have the module's exports take callbacks. I mean come on.

Speaking of which, how would you feel about a lightly patched V8 engine appearing as a PR in this repo?

scf4 commented 5 years ago

Ah I understand now, and you make some good points. How does the API prevent this usage though? Could you provide an example of how lib/add.js should look?

As for require, we could simply redefine the require function. I think that would be within the scope of this project since #7 is also on our roadmap.

scf4 commented 5 years ago

This works:

global.__require = require.bind();

global.require = require = function require(...args) {
  if (args.length === 1) return __require(args[0]);
  return args[1](null, __require(args[0]));
};

// Test:

require('callbaxx', function (err, callbaxx) {
  callbaxx.map([5, 15, 25, 35], callbaxx.add, function (e, r) {
    console.log(r);
  }, true, 5);
});
ndorf commented 5 years ago

Beautiful.