-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: Callback hell solution: backcalls #64
Comments
I do not like this. It goes against the CoffeeScript indentation semantics. Why would this:
Be clearer than:
|
I'm no expert, but I think the idea is to avoid deep nesting of callbacks. Instead of the current $.get 'ajaxtest', (data) ->
$('.result').html data
$.get 'ajaxprocess', data, (processed) ->
$('.result').append processed you could write data <- $.get 'ajaxtest'
$('.result').html data
processed <- $.get 'ajaxprocess', data
$('.result').append processed (based on an example from LiveScript docs) This seems neat, but I wonder about two things: how do you specify which argument to put the callback (LiveScript uses One other thing: the operator @celicoo is proposing is apparently |
async/await |
@lydell Nice find. Presumably, any discussion should continue over there. @DomVinyard I'm still learning Promises/async/await (I was corrupted too early by IcedCoffeeScript and now I can't figure it out). Can the example above be written cleanly with async/await? It definitely can with IcedCoffeeScript: await $.get 'ajaxtest', defer data
$('.result').html data
await $.get 'ajaxprocess', data, defer processed
$('.result').append processed Hopefully I'm not too biased toward IcedCoffeeScript which I already know, but to me this feels a lot more clear notationally, in particular which argument is the callback. Also you can do awesome things like |
@mitar The main idea of backcalls is to avoid callbacks hell, as the title (and @edemaine) suggest, i use Livescript and it is fantastic. @DomVinyard I don't think it's necessary to compare backcall's benchmark to async/await to know that backcalls are definitely faster, once it doesn't generate any kind of code, it's just pure callbacks. |
Hm, so if the idea is just to remove indentation level, maybe we could just have an operator for that? Like:
|
Any callback is trivially converted into a Promise. To use @edemaine's example: $.get 'ajaxtest', (data) ->
$('.result').html data
$.get 'ajaxprocess', data, (processed) ->
$('.result').append processed That could be rewritten into: new Promise (cb) -> $.get 'ajaxtest', cb
.then (data) ->
$('.result').html data
new Promise (cb) -> $.get 'ajaxprocess', data, cb
.then (processed) ->
$('.result').append processed |
Not if the first argument of the callback is an error. |
@mitar, why not? Can you give an example? |
I mean, if you just blindly pass promise's |
Of course. Nothing should be done blindly. It has to match the method signature. |
So, can you show the above example of converting to Promises with function calls which expect node.js style callbacks with errors as first arguments? Maybe then is clearer why backcalls are cleaner. |
I'm not familiar with node.js style callbacks. If you provide an example, I'll rewrite it to promises, and we can see if it's doable. |
Here's a couple of ways I can think of: # Function type 1
fOne = (value, cb) -> # something
new Promise (cb) -> fOne 42, cb
.then (error, result) ->
if error? then alert "Oh noes: #{error}"
else alert result
# or
new Promise (cb) -> fOne 42, cb
.then (errorOrResult) ->
if resultOrError instanceof Error then alert "Oh noes: #{resultOrError}"
else alert result alternatively: # Function type 2
fTwo = (value, errCB, resCB) -> # something
new Promise (resCB, errCB) -> fTwo(42, errCB, resCB)
.then (result) -> alert result
.catch (err) -> alert "Oh noes: #{err}" |
Vote to close with no action |
Reminds me a lot of jashkenas/coffeescript#4144, with many of the same issues that that proposal had. |
#4144 feels a lot better imo |
Really love to see jashkenas/coffeescript#4144. |
Migrated to jashkenas/coffeescript#4957 |
Would be great if we had backcalls as livescript:
CS6:
ES6:
The text was updated successfully, but these errors were encountered: