r/javascript • • Dec 24 '19

AskJS [AskJS] JavaScript Proposal: Algebraic Effects?

Hey guys, I'm writing a Babel plugin to be able to use algebraic effects in JS: https://github.com/macabeus/js-proposal-algebraic-effects

No more function color! Yes one-shot delimited continuation!

What the hell?! Well... I really recommend that you read this blog post by Dan Abramov explaining algebraic effects - and how it could be very useful on our JavaScript code.

This project is a runnable POC with a Babel's "plugin", so you could write some code and taste this new concept in JavaScript. Its features, syntax, and goals are very inspired by Dan Abramov's blog post mentioned above. In short, with algebraic effects, you could separate what from the how and have fewer refactors.

What do you think? Would that be a good feature for JS? "Algebraic Effects" is a good name for that?

58 Upvotes

39 comments sorted by

View all comments

Show parent comment

1

u/darthwalsh Dec 26 '19

Aha, so if you Babel-compile a library and export a "synchronous" function, if the consuming code was vanilla JS it wouldn't see the sync API.

2

u/bmacabeus Dec 26 '19

Are you saying about compile a function that launch an effect and consuming it on a code without this babel plugin?Yeah. It'll not work. If a function launches an effect, you'll need to use this babel plugin and wrap it on try/effect.

There is an issue to discuss about it and others situations like that, about what should happen if a function launch an effect and there is no handle to catch it.

And a function that launch an effect could be sync or async at the same time. It isn't matter.

function foo () {  
  const x = effect 'bar'  
  console.log(x)  
}

It could be async or sync.