LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Using backcalls with Promises

Open mayeaux opened this issue 10 years ago • 18 comments

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.

mayeaux avatar Jul 11 '15 06:07 mayeaux

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    

robotlolita avatar Jul 11 '15 15:07 robotlolita

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?

mayeaux avatar Jul 11 '15 17:07 mayeaux

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?

robotlolita avatar Jul 11 '15 18:07 robotlolita

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.

vendethiel avatar Jul 11 '15 18:07 vendethiel

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:

gabeio avatar Jul 11 '15 19:07 gabeio

@robotlolita why don't u use LS anymore?

iyeldinov avatar Jul 12 '15 11:07 iyeldinov

@gabeio I don't know either. Hahaha.

@mrmayfield Take a look at this: https://github.com/paulmillr/LiveScript.tmbundle/pull/39

blvz avatar Jul 22 '15 03:07 blvz

@iyel I'm using Sweet.js for writing JS (because macros), and otherwise concentrating the efforts on my own PLs.

robotlolita avatar Jul 22 '15 13:07 robotlolita

@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)

heavyk avatar Jul 22 '15 13:07 heavyk

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).

vendethiel avatar Jul 22 '15 13:07 vendethiel

@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

robotlolita avatar Jul 22 '15 13:07 robotlolita

Oh, yeah, I forgot about custom readtables. Those would be very hard to add to LS...

vendethiel avatar Jul 22 '15 13:07 vendethiel

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

heavyk avatar Jul 22 '15 13:07 heavyk

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.

vendethiel avatar Jul 22 '15 13:07 vendethiel

does not like code pyramids - uses promises.

heh.

igl avatar Sep 13 '15 17:09 igl

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!

dead-claudia avatar Oct 22 '15 05:10 dead-claudia

And related: #683

dead-claudia avatar Oct 22 '15 05:10 dead-claudia

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

JoyKrishnaMondal avatar Nov 28 '15 21:11 JoyKrishnaMondal