283 points · by JoshGlazebrook · 11y ago · 105 comments
...which is cool, because using async/await in C# made me realize how much simpler dealing with async code could be (compared to my experience with JS callbacks or Java threads).
http://blogs.msdn.com/b/vcblog/archive/2014/11/12/resumable-...
Go's model doesn't give you as much control over which thread a particular operation gets assigned to. It's not theoretically impossible, I suppose, but it's hard to imagine what a simple API for it would look like.
But yes, if this particular problem were solved in an elegant way, I would also prefer the CSP model. It makes it so you don't have to worry about which operations are async or not.
In any case I suppose many styles of Go program will exhibit accidental determinism, but it still seems too difficult to depend on as randomness can be introduced simply by invoking the wrong function, or by using a different runtime version, which to me feels comparable to the hazards of raw multithreading.
While async/await allows for an abstraction over the lower level tasks, that are still exposed, in case one needs more control over the threading runtime.
For example, in JS's case you can still dig into the Promise world if needed.
Similarly in C#, Tasks / Dataflow and SignalR can also be used.
Whereas in Go, correct me if I am wrong, outside CSP one needs to reach out to threads/mutex/semaphore directly.
I'm not sure I understand your sentence. Are you saying that communicating sequential process only works because it is first class in Go or that CSP is not a solution to every concurrency problem ?
What does streaming APIs have to do with CSP per se?
var orders = await getJSON('/users/joe/orders');
No callbacks, promises etc (Edit: that you have to look at, as other posters note the implementation uses promises in the background).You still have to explicitly say you want to be async, so it's not quite as good as non-blocking IO in something like Elixir, but it's the future as far as JS goes.
Which is unfortunate because async/await only works with promises, and if you want any other async model (like one that doesn't eat errors all the time) you can't use it. Luckily, generators are fine.
This is the same as saying that "arrow functions only works if you want to represent functions".
Also, promises don't eat errors. It's the code using those promises that forget to handle them.
The async/await fixes that by unwrapping those errors for you. Any errors in an "await expression" will be thrown as exceptions up the stack.
Promises are not the only way to do async work in JS, as much as some people like to say that. You have observables and channels, which are more powerful because they handle streaming.
There are proposals to make async/await more composable: https://github.com/jhusain/compositional-functions. TC39 is aware of this but I'm not sure if they are going to fix it or not.
> Also, promises don't eat errors. It's the code using those promises that forget to handle them.
They literally do. They run your code in a try/catch block and store the error away, and you have to remember yourself to manually throw it later. You shouldn't be forced to run code inside a try/catch. http://jlongster.com/Stop-Trying-to-Catch-Me
try {
await do_thing();
} catch {
// works
} // this
var x = await promise;
callSomething(x);
// translates as
promise.then(x => callSomething(x));
Under the hood, the entire "async" function is turned into a state machine instead of creating a lot of callbacks. It's more efficient it can do some optimizations that would be boring to do manually.For example, if the translation were simply "turn into callbacks", this:
var p = getJson();
var x = await p;
var y = await p;
var z = await p;
would translate to something like : var p = getJson();
p.then(o => { var x = o; });
p.then(o => { var y = o; });
p.then(o => { var z = o; });
but instead, the code runs more like this: getJson().then(o => {
var x = o;
var y = o;
var z = o;
});
I say runs like because the compiled code is more complex than that, it will still call functions, etc. It will also handle exceptions, which would add a lot of boilerplate to this code. But have in mind that because this is generated by the compiler, this code will in most cases be more efficient than anything you try to write by hand.The similar line they use in their examples doesn't define a separate getJsonAsync function in the "Asynchronous city" section (and, as far as I can tell, there's no native getJSON function). That, and the fact that the line immediately preceding the await call has a comment that says, "just have to await the promise!", leads me to have to assume they used the function defined in the promises section, which of course uses promises. And, in order to define that promise, they do have to use three callbacks.
With functions and just callbacks I can make one-liners for fetching data too if I abstract away all the stuff that actually does the fetching of the data:
getJSON('/users/joe/orders', function(orders) {} );There is a standard, native fetch() function which is pretty darn similar: https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/
All async functions return promises, and all await expressions await on promises. Currently on babel implementation with facebooks's regenarator it seems to work on any "thenable", because I got it working with jquery and angular promises.
Promises are sufficiently supported in the latest versions of all major browsers, and Generators are almost there. They're supported in Firefox and Chrome, behind a flag in Edge, but unsupported in Safari.
But if you're targeting Node 4.0, you might as well transpile down to ES6 instead of going all the way to ES5 and pulling in Regenerator :)
This means you can get await-like promise handling in Node.js using the --harmony_generators flag.
If you're writing a library, use the runtime option. If you're writing a website or other standalone app, you should use the polyfill (especially if you use other libraries like React which depend on ES6 globals too).
[1]: https://github.com/ubolonton/js-csp [2]: http://taskjs.org/ [3]: https://github.com/tj/co [4]: https://github.com/bjouhier/galaxy [5]: https://github.com/creationix/gen-run
Edit: it appears that the caolan async lib came out after yours. Sorry for the confusion.
Your first commit was that January and Caolan's was that May, and when yours was "out" is debatable anyway since you don't have any releases tagged.
I would hardly feel obligated to rename a project over a 4-month-old Github repository that doesn't even target the same platform (Firefox vs. async being originally Node-only).
Which argues that async/await might not be what we really want in the end. As a user of Promises and async/await in the future, it's a very interesting read!
Even in Java UI frameworks like on Android or Swing, you have an event loop with a single thread, and when spawning another thread you have to deal with special communication layers [1]. True multithreaded UI's seem like an area for research.
[1] https://developer.android.com/training/multiple-threads/comm...
https://github.com/koajs/compose/pull/27#issuecomment-144868...
async/await is important and highly desired by the community, but I would prefer if it were not Microsoft trying to blast ahead and create yet another separate fragmentation between browsers. Sure they call it an experimental feature, but how long until they decide, "well we're not going to wait for this to get finalized, we're releasing it for everyone!!!" ?
Still a ways to go, though...
As a side note, I don't like how async/await has to be wrapped in try/catch AND a function for proper error handling
(async ()=>
try {
let result = await fetch('/file.json');
let json = await result.json();
console.log(json):
} catch(error) {
console.error('Not sure fetch failed or toJson');
}
)();
if you want to handle each error separately you need even more try catch!As for the try catch, you don't NEED to wrap. The sample just shows you can. You can't do:
try {
promise.then(() => ...);
} catch {}
but you can do try {
await promise;
...
} catch {}
async/await is a way to write simpler code the same way you would use a "for" block instead of a "while(iterator.next())" - because it's easier, all the rest is still the same.Of course, the same way you need to add a second callback to a promise handler if you want to handle errors. There is no difference, really. You're just changing the way you write the code.
Also, you're not forced to choose "await or promises". You can use both as you see fit, in the same block:
async () => {
// make 2 requests in parallel
var p1 = fetchSomething();
var p2 = fetchAnotherThing();
// wait for both of them to be done
await Promise.all(p1, p2);
// continue when they're both resolved
// these "await" only get the values since the promises are resolved
doSomethingWithTheValues(await p1, await p2);
}
> But for sync functions we usually let the error terminate the program because if a sync function throws in runtime it means something is wrong in my program, not in networkingThis is just the way you're choosing to see the problem. Promises can be used for anything, it's not tied to networking. I usually write promises that will be resolved when a modal window closes or when an application event happens.
It's just a simpler way to write code. The way you use it is up to you.
Also, async/await makes debugging a lot easier. The debugger can understand that the next expression after an "await" is supposed to be in the same function context. No more adding breakpoints to the callback function because only you know how the code is called at runtime. You debug async functions as normal functions.
while(!iterator.next().done)
I think you meant the above example and not the one you listed in the comment as generator objects in JS always return objects upon calling next method even after finish yielding for long and thus you could have an infinite loop on your hand here."F# to JavaScript with type providers
FunScript is a lightweight F# library that lets you rapidly develop single-page applications. You can connect to external data sources and call REST APIs with intellisense, produce dashboards using JavaScript visualization libraries and write asynchronous computations easily without explicit callbacks."
Functions that contain a try/catch cannot currently be optimized by V8. If we migrate to using try/catch rather than `.then/.catch`, I wonder if there's going to be any perf implications since previously optimizable functions may no longer be optimized.
socket.on("data", echo); // calls the echo function when data arrivesIt is really a relief when it comes to multiple AJAX/UI dependant logic eg.
if(await showDialog("get coffe ?") == "yes"){
results = await getCoffeFromServer();
}
else {
...
}Get the basics done right first imvho. I don't want developers staying late to try and understand why something isn't working in a MS browser anymore.
Seriously though. I think that argument is losing steam every time I do testing with Edge. It gets updated. It feels different.
Unless somebody were to tell me both IE and Edge belong to the same browser family, I wouldn't be and to tell.
I'd say it's fair to call it a new browser.
My point being: IE is just IE. Edge is Edge and we're happy about it.