-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Features: async/await #10
Comments
Implicit async if await is detected in the method body +1 |
Agree with @DomVinyard on this one. This is what IcedCoffeeScript is doing. |
async functions don't necessarily contain async function () {
return Promise.resolve(1)
} ... but, they also don't need the async keyword. So I think I support implicit |
Basic support is shipping in several browsers: tc39/proposal-async-await#97 (comment) |
Personally, I find the fn() ->
res = await fetch('url')
await res.json() What about something like this instead: fn() ->
res <= fetch('url')
<- res.json() Where Thoughts? |
Ah, |
@rattrayalex i think if we were trying to mess with 'await' as a keyword, the only sensible 'true coffeescript' approach would be to go the other way and make it the more verbose 'wait for', rather than trying to get people to memorise a new operator.
But that has a whole bunch of issues, not least of all the overloading on the word 'for'. Personally think should be left untouched.
Is such a wonderful reminder of how far JS has come, isn't it. |
I am leaning in the direction of leaving In a world with a |
amen, brother. and
Yeah, absolutely. +1 here. |
Why can't we just use syntax with |
@octet-stream verbosity. 😉 |
To elaborate, the async -> await bar() which looks like you're doing this: async(function(){ await bar(); }); which you're not. CoffeeScript's two-character function definitions enable more expressive, functional programming styles, and make JavaScript (arguably) more readable and (absolutely) more lightweight. Requiring use of the Most importantly, whether or not a function is asynchronous can be inferred by whether it contains an The one reason one might want to include |
As for "why introduce new syntax for So code that used to look like this: foo() ->
fetch(someUrl)
.then resp ->
resp.json()
.then json ->
someTransform(json) can now look like this: foo() ->
resp = await fetch(someUrl)
json = await resp.json()
someTransform(json) or, better yet: foo() ->
resp <- fetch(someUrl)
json <- resp.json()
someTransform(json) ... which has a substantial keystroke savings, is arguably more clear/readable, and doesn't look like a function call. In general, CoffeeScript is very light on keywords, which is great because function calls look like keywords. |
I'm not sure, but how about adding these operators as aliases for Koa = require 'koa'
koa = new Koa
async koa.use (ctx, next) ->
ctx.body = 'Hello, world!'
await do next
koa.listen 2319, -> console.log 'Koa started on http://localhost:2319' or Koa = require 'koa'
koa = new Koa
koa.use (ctx, next) ~>
ctx.body = 'Hello, world!'
<~ do next
koa.listen 2319, -> console.log 'Koa started on http://localhost:2319' |
@octet-stream why does Note also this would prevent Slightly OT, but I also believe the |
Other than the fact that Thoughts? |
I'm just used sayHello = (name = 'Nick') -> console.log "Hello, #{name}!"
do sayHello # -> Hello, Nick!
sayHello "Alex" # -> Hello, Alex! I think this code looks more pretty than
Maybe just for alternative asynchronous function declaration syntax? |
Ah, silly me. Thanks.
What's wrong with just |
I would definitely go for a simple function declaration as before. First, as said before it matches the way CoffeeScript manages generator (inference from the yield usage). Second, it will not require you to care about removing Oh, and concerning |
Prefer implicit
|
Yes, I think we can implicit |
Use facebook regenerator if you want working example :) |
I’m working on the document that puts in one place our consensus plans. To confirm that I’m understanding this correctly:
So to take the example from http://stackabuse.com/node-js-async-await-in-es7/, this JavaScript: var request = require('request-promise');
async function main() {
var body = await request.get('https://api.github.com/repos/scottwrobinson/camo');
console.log('Body:', body);
}
main(); would be written in CoffeeScript as: request = require 'request-promise'
main = ->
body = await request.get 'https://api.github.com/repos/scottwrobinson/camo'
console.log 'Body:', body
main() ? I think using |
Also, is there anything we lose by not supporting this feature? Any JavaScript library or framework that can’t be used because we can’t do I noticed that |
It reached stage 4 a few months ago: https://github.com/tc39/proposals/blob/master/finished-proposals.md . So all of the details have been finalized and it will be in ES2017. My guess is that the "Debatable Syntax & Semantics" part of the page you linked is just out of date. |
@zeekay pointed out this PR which seems to be exactly what we want to implement, though it should be revised to remove the ES5 shim. Based on the precedent of generators and modules, I think our consensus now is that ESNext features are output as ESNext, especially if they’re opt-in by using a special syntax. But this feature may be very close to implementation based on the work in the PR. |
One question. Is there ever a reason that developer would call an async function without await? Because if there is not one, then we could really easily do something where we mark a function as async, which would do two things:
So what I would like from CoffeeScript is to make all this magical. So maybe we could have:
Observe Here, a good thing is that a return value of the Promise's Now, I dislike the fact that we have to write
So because Alternatively, we could do (a list of different syntaxes):
(Here Alternative:
Or:
BTW, I think just having |
Sorry forgot to close this. This feature has been implemented. |
Hm, OK, but what about discussion about making async/await more integrated with the language? Should I open another issue for that? |
Sure, but please review what’s already been built so far regarding |
If I understand, currently there is only |
I opened #60. |
Issue migrated to jashkenas/coffeescript#4908 |
The syntax here could be quite tricky, and the feature is pretty important. This could be an opportunity for CoffeeScript to offer dramatically nicer syntax than ES6 for a core feature.
Let's discuss anything related to async/await here.
The text was updated successfully, but these errors were encountered: