Using backcalls with Promises
I have some code that runs a Promise on repeat.
putOnRotation = (callback, delay) ->
return callback delay
.then (data) ->
console.log(data)
return Promise.delay(delay).then ->
return putOnRotation callback, delay
.catch (error) ->
console.log(error)
return Promise.delay(5000).then ->
return putOnRotation callback, delay
However, I don't like this pyramid pattern, and would prefer to use backcalls.
I've ran into a litany of issues getting them set up, any ideas on how I could remove the indentation of this function using backcalls? Thanks.
put-on-rotation = (f, delay) ->
do
data <- f delay .then
console.log data
_ <- Promise.delay delay .then
put-on-rotation f, delay
.catch (err) ->
console.log err
_ <- Promise.delay 5000 .then
put-on-rotation f, delay
Thanks a lot! But what are you using for syntax highlighting? I am using this Sublime Text bundle: https://github.com/paulmillr/LiveScript.tmbundle but the highlighting looks pretty horrific haha. Missing most of LS features and promises are not well understood either, any better suggestions?
I don't use LS anymore (used to use the Emacs mode back then). Don't know any tool with good support for LS, maybe @vendethiel knows?
I havn't used LS for a while (I've thankfully been able to stay away for JS), but I was using the tmbundle as well. I know the vim mode works rather well.
I currently use Atom.io with @blvz's livescript highlighter language-livescript-edge idk if it has all the features highlighted but it's sufficient for me :) :+1:
@robotlolita why don't u use LS anymore?
@gabeio I don't know either. Hahaha.
@mrmayfield Take a look at this: https://github.com/paulmillr/LiveScript.tmbundle/pull/39
@iyel I'm using Sweet.js for writing JS (because macros), and otherwise concentrating the efforts on my own PLs.
@robotlolita what sorts of macros? wouldn't it be possible to do the same in LS? they don't look that difficult to emulate (I think)
When I tried to add custom operators to LS, a big problem was that we'd have to require dually-spacing them (because of ambiguities).
@heavyk pattern matching, ADTs, do notation, DSLs (like JSX embedded in the source code), mostly.
Some examples: https://github.com/folktale/text.pretty-printing/blob/master/src/index.sjs#L99-L112, https://github.com/folktale/wrapper.express/blob/master/examples/webservice/index.sjs#L44-L62, https://github.com/robotlolita/raven/blob/master/source/scripts/screens/entry.sjs#L143-L167, https://github.com/robotlolita/raven/blob/master/source/scripts/novel.sjs#L97-L111
Oh, yeah, I forgot about custom readtables. Those would be very hard to add to LS...
oh shit, that is sweet... you have inspired me to have a look at it more closely.
at work we use ES6 transpiled js with babel (pretty cutting edge, but no way I could convince them to do coffee or ls). adding another transpiler won't be an issue. this may be the solution to my 100 zillion functions :)
thanks for the inspiration! I LIKE THAT A LOT!!
@vendethiel - hmm yes, now that I think about it that would be a total pain in the ass. sometime in the future we will need to make more context aware languages to bridge that gap. (sorta like in english where some words sound the same but mean different things, and other times the same word can mean a variety of things depending on the context)
now to explore sweet... I'm really excited
at work we use ES6 transpiled js with babel (pretty cutting edge, but no way I could convince them to do coffee or ls). adding another transpiler won't be an issue. this may be the solution to my 100 zillion functions :)
I'd really recommend that, I think Sweet support ES7 syntax as well.
does not like code pyramids - uses promises.
heh.
Have you all seem my gist? It's made for Node-style callbacks.
Well...as for Promises, it's easy to nodeify them:
nodeify = (promise, callback) !-> promise.then (-> callback null, it), (-> callback it)
Here's one of the above examples with my gist:
# put-on-rotation f, delay, callback
put-on-rotation = (f, delay, callback) ->
error = async (await, err, next) ->
console.log err
_ <- await nodeify, Promise.delay 5000
put-on-rotation f, delay, next
async do
(await, next) ->
data <- await nodeify, f delay
console.log data
_ <- await nodeify, Promise.delay delay
put-on-rotation f, delay, next
(err) -> if err? then error err, callback else callback!
And related: #683
https://github.com/JoyKrishnaMondal/GetRidOfError
After reading this thread I wrote this tiny module to assist when backcalls are used.
Tell me how terrible it is lol