6 ms·
Show HN: Callbaxx.js – A utility library to bring callbacks to synchronous code
- Gabriel_Martin 8y agoI love how no one is getting zealotous about this :)
- jtokoph 8y agoSo now to get really deep/meta, you'll want to combine with promisify: var isTrue = require('callbaxx').isTrue; var promisify = require('promisify'); var promIsTrue = promisify(isTrue); promIsTrue(true) .then(function (res) { console.log(res) }); .catch(function (err) { console.log(err) });
- scottmf 8y agoHi! That's definitely an interesting experiment, but it's rather antithetical to the project. In fact, it might even be broken by future versions of the library. See issue #7 [1] for a look at why this might not be a good idea. Thanks anyway! 1. https://github.com/scf4/callbaxx/issues/7 https://github.com/scf4/callbaxx/issues/7
- jcolella 8y ago@jtokoph, maybe this can be put in the examples for the `callbaxx-promise` library that will be surely be created after.
- leetbulb 8y agoWe must go deeper! const promisify = require('bluebird').promisify const from = require('rxjs').from const isTrue = require('callbaxx').isTrue const isTrueAsync = promisify(isTrue) const makeIsTrueSource = val => from(isTrueAsync(val)) const source = makeIsTrueSource(true) source.subscribe(console.log, console.error)
- z3t4 8y agoA few more abstractions and it will meet enterprise compliance. Probably need to write it as a Class ...
- gtsteve 8y agoGiven some of the JS code I've seen recently, I honestly thought this was a serious library until I saw this: > Proponents of this new language (known as "sixers")
- asprouse 8y agoSixers unite!
- e1g 8y agoThe satire might be inspired by a very real lib 'Callbag" from the creator of Cycle.js https://github.com/staltz/callbag-basics https://github.com/staltz/callbag-basics
- scottmf 8y agoI haven't seen that library, but a HN comment chain from last July[1] (nailer's reply in particular) almost certainly planted the seed for this. 1. https://news.ycombinator.com/item?id=14871580 https://news.ycombinator.com/item?id=14871580
- rounce 8y agoI think they're unrelated. This seems to be a protest repo against JavaScript's churn.
- rounce 8y agoOhhhh I've been had. That'll teach me to RTFM.
- iamleppert 8y agoWhen I think of ES6 I think of this statement: “Appreciation of beauty is taste and the creation of beauty is art.” ES6 is lacking in all forms of taste and art is never the product of design by committee.
- exabrial 8y agoYour last sentence, needs commas.
- GordonS 8y agoAnd ironically, yours does not!
- exabrial 8y agoIndeed. Subtle humor doesn't work on hn.
- quickthrower2 8y agoNice comma usage.
- TeMPOraL 8y agoLooking at the API, this encourages CPS[0], doesn't it? Also, great work. I had a good laugh, and I can't wait for the inevitable day when someone actually uses this in production. -- [0] - https://en.m.wikipedia.org/wiki/Continuation-passing_style https://en.m.wikipedia.org/wiki/Continuation-passing_style
- deleted 8y ago[deleted]
- greenshackle2 8y agoJavaScript, finally fully embracing its Lispy roots.
- paulogdm 8y agoFor me the best part is the examples: https://github.com/scf4/callbaxx/tree/master/examples https://github.com/scf4/callbaxx/tree/master/examples Also, someday I think we need to launch an API (REST or Graph) to do some basic operations as API calls. So you don't even need a whole language, just lots of fetchs.
- paulogdm 8y agoAnd it keeps getting better: https://github.com/scf4/callbaxx/pull/12 https://github.com/scf4/callbaxx/pull/12
- scottmf 8y agoThat's a fantastic idea. What about a babel plugin to transform all code into fetches to a proprietary REST API? By doing all of the work for them, we'd ensure reliability with common operations like + and /, which obviously means we eliminate the need for tests! This would allow devs to move and break things even faster, while we/I collect $10–$20 per 10,000 API calls. E.g., // Regular code console.log(3 + 2); // Transpiled code console.log(await fetch('http://fetchify.io/add/3/2')); // Regular code const isFive = (x === 5); // Transpiled code const isFive = await fetch(`http://fetchify.io/equality/strict/${x}/5`) .then(body => body.text()); Some of this is almost too easy to understand though, therefore it's pretty amateurish and we're not doing proper programming. What about something like: // Regular code const thirty = 5 * 6; // Transpiled code let $$_tmp1; const thirty = ( $$_tmp1 = await fetch(`http://fetchify.io/multiply/5/6`), $$_tmp1 = await $$_tmp1.text(), $$_tmp1 = parseInt($$_tmp1, 10), $$_tmp1 ); Even some seasoned developers aren't aware of this syntax, so it's perfect.
- draw_down 8y agoGood grief.
- gitgud 8y agoI hate how convincing this is. It makes me rethink my optimism on new libraries...
- z3t4 8y agoWriting callbacks is much more fun if you make use of closures and named functions.
- quickthrower2 8y agoI was going to say this must be inspired by bored Haskellers
- scottmf 8y agoBut what's the point of writing callbacks if you're not going to endlessly nest them? E.g., I can tell the nested math example[1] is good code because of how deeply indented it becomes. 1. https://github.com/scf4/callbaxx/blob/master/examples/nested-math.js https://github.com/scf4/callbaxx/blob/master/examples/nested...
- z3t4 8y agoIgnoring that the code doesn't handle any errors, there's really nothing wrong with nesting, as you can clearly see the code flow and understand what it does. The example (1) would look much better using Promise's as it's a chain of events. But imagine a horizontally scaled distributed system where each math operation run's on a load balanced cluster, you would need code to handle back-pressure, retry when errors occur, etc. Here's how FizzBuzz might look like: function fizzBuzzLogic(nr, multipleOfThree, multipleOfFive) { var print = console.log; if(multipleOfThree && multipleOfFive) print("nr=" + nr + " FizzBuzz"); else if(multipleOfThree) print("nr=" + nr + " Fizz"); else if(multipleOfFive) print("nr=" + nr + " Buzz"); else print("nr=" + nr); } function fizzbuzz(cb) { var modulo = require("modulo"); var errors = []; var maxErrors = 3; var maxConcurrency = 5; var inProgress = 0; var fizzBuzzCounter = 1; next(); function next() { while(cb && inProgress < maxConcurrency && fizzBuzzCounter < 100) run(++fizzBuzzCounter); } function run(nr) { console.log("run: nr=" + nr); var multipleOfThree = undefined; var multipleOfFive = undefined; moduloTest(3); moduloTest(5); function moduloTest(multipleOf) { inProgress++; modulo(nr, multipleOf, moduloResult); function moduloResult(err, moduloNr) { inProgress--; if(err) { errors.push(err); if(errors.length >= maxErrors) { if(cb) cb(errors.join("\n")); cb = null; } else if(cb) moduloTest(multipleOf); } else { if(multipleOf==3) multipleOfThree = (moduloNr==0); if(multipleOf==5) multipleOfFive = (moduloNr==0); if(multipleOfThree != undefined && multipleOfFive != undefined) { fizzBuzzLogic(nr, multipleOfThree, multipleOfFive); next(); } } } } } } I haven't polished or run the code, it for example doesn't call the cb (which is a common error in callback based code), but you get the idea. As an exercise you could rewrite it using async *function or using Promises, but I believe the benefit of Promises wouldn't be as apparent.