5 ms·
For anyone unfamiliar with JS, async/await is about the best JavaScript will ever get: var orders = await getJSON('/users/joe/orders'); No callbacks, prom
by nailer 11y ago
For anyone unfamiliar with JS, async/await is about the best JavaScript will ever get:
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.
- xlm1717 11y agoI'm not sure about the no promises and no callbacks part. 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) {} );
- nvivo 11y agoThe parent exaggerated a little. async/await does use promises. In fact, it's built around them. 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.
- callahad 11y agoDepending on which transforms you enable, you can also eliminate Regenerator and use native ES6 promises and generators. E.g: https://gist.github.com/callahad/b99c83d6d9fd675137b7 https://gist.github.com/callahad/b99c83d6d9fd675137b7 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 :)
- callahad 11y ago> as far as I can tell, there's no native getJSON function There is a standard, native fetch() function which is pretty darn similar: https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/ https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/
- jlongster 11y agoThere are promises there, it just hides it. All async functions return a promise, and `await` works with promises builtin. It's basically the same thing as generators, without the `function*` syntax. 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.
- nailer 11y agoThat's correct, I've edited my post to be clearer. Thanks.
- SerpentJoe 11y agoI'm confused, I don't know what type of errors a promise eats. If you're chaining together promises returned by other routines, especially with async/await, then error propagation and handling is very natural. If you're creating a promise using `new Promise` then you can use reject to propagate errors. How does this fall short of other async models?
- grayrest 11y agoThe problem isn't the propagation, it's the propagation by default. He wants errors that occur in development to pop a stack trace as soon as the error occurs without needing to attach a .catch handler.
- spb 11y agoBut... it's an async call. The error is going to happen as part of the event loop. That's inherent to the nature of async. If you want to get the error before it happens, you'll need a time machine.
- colin_jack 11y agoIts not about getting errors before they happen: http://jlongster.com/Stop-Trying-to-Catch-Me http://jlongster.com/Stop-Trying-to-Catch-Me
- Kiro 11y agoWhat's so good about it? I still want to fire some kind of callback when it's done. Or am I missing something?
- smcl 11y agoI can't see either - would be nice to see\understand a little more what's so special about it.
- mateuszf 11y agoWhen it's done, you will have the value in orders variable. If you can do something with it - just do it in new line in the same scope. You can also use catch exceptions using standard try catch block.
- Loque 11y agoIt's just sugar, and allows people to write JS do to this in a more consistent manner.
- nailer 11y agoYou only want to fire a callback because that's how you did async previously. In reality, all you want is async.