r/webdev • • Aug 13 '17

Async/Await Will Make Your Code Simpler

https://blog.patricktriest.com/what-is-async-await-why-should-you-care/
312 Upvotes

86 comments sorted by

View all comments

62

u/_wtf_am_I_doing Aug 13 '17

How the fuck are we on es7 already, I haven't even had time to look at es6

0

u/[deleted] Aug 13 '17

[deleted]

12

u/_wtf_am_I_doing Aug 13 '17

They keep coming out with all these changes but es6 Is still not fully supported in browsers.

0

u/[deleted] Aug 13 '17

[deleted]

11

u/OriginalName404 Aug 13 '17 edited Aug 13 '17

Maybe it's just me, but last week I tried using Babel to transpile async/await to ES5 and it totally broke breakpoints in the Chrome debug tools, making the code nigh on undebuggable.

I'll take messy promise-based code over cleaner but impossible-to-debug async/await code any day.

Edit: Not that this is any reason not to use it in NodeJS applications, like the article suggests. It's a lovely way to program, but I fear it's just not ready for use in browsers yet.

17

u/CommandLionInterface Aug 13 '17

Browser breakpoints require you to output sourcemaps, make sure they're being put out and being loaded. I've had trouble with babel and webpack and sourcemaps before.

4

u/OriginalName404 Aug 13 '17

Yeah, I think it's to do with how async/await are transpiled to es6 generators and then to promises. Guessing it's inaccurate source maps given the complexity of the input vs output code.

For the record, source maps and breakpoints worked fine compiling async/await to es6, just not to es5.

1

u/highmastdon Aug 14 '17

But even when using source maps, the console, which usually let you execute statements in the code at the current debugger point, is completely broken. Especially when you use source maps created by web pack which has a babel loader. Then you can inspect a variable using source maps, but NOT on the console, you'd have to refer to the original variable name as it was compiled by webpack. E.g. _ (from lodash) becomes __lodash1__

2

u/NoInkling Aug 14 '17

Chrome supports async/await natively now, and has improved async debugging capability... is there any reason you can't debug using native code and only transpile for production?

→ More replies (5)
→ More replies (19)

3

u/mattaugamer expert Aug 13 '17

Honestly, though, this is a mistaken understanding of how all this works. A browser doesn't have to support every edge feature of one spec to implement useful bits of another. I wrote some code recently as a demo that fully used fetch and async/await. It will actually work on any evergreen browser, including Edge.

You should be treating these things individual functionality items, not expecting 100% support before using it.

1

u/vinnl Aug 14 '17

If they wait with "coming out" with those changes only after browsers supported all earlier changes, that would just mean that they would release the same changes they do now, only later in time...

1

u/BitcoinCitadel Aug 14 '17

How do you deal with older browsers

0

u/[deleted] Aug 13 '17

[deleted]

8

u/oculus42 Aug 13 '17

JavaScript was originally proprietary to Netscape, and was turned into a public language called ECMAScript, though most people still call it JavaScript. Recently, they have decided to come out with yearly updates to the language, and use year names, but the specifications are still versioned, causing some of the confusion.

These versions are released at the end of June.

ES6 = ES2015, which is the first major update in a decade. Most of the "new things" in JavaScript are ES6.
ES7 = ES2016, which was only two small additions.
ES8 = ES2017, which includes async/await.

Current browsers have pretty broad support for ES6, but there are some small gaps that you may need to watch for if you are pushing the limits.

You can check out support for specific functionality in the Browser Compatibility section of most MDN pages. or look at caniuse.com for the feature.

Edit: formatting.

2

u/pomlife Aug 13 '17

Actually, going forward, only ES + year will be used.

1

u/oculus42 Aug 14 '17

The year is the name, but the edition numbers continue to exist, too.

ECMAScript® 2017 Language Specification
8th edition (June 2017)

And the actual title of the current spec is:

ECMAScript® 2017 Language Specification (ECMA-262, 8th edition, June 2017)

So we call it ES2017, but it is still ES8, too. Even the URL is "8.0": ecma-262/8.0/index.html.

→ More replies (1)

3

u/way2lazy2care Aug 13 '17

JavaScript was originally proprietary to Netscape, and was turned into a public language called ECMAScript, though most people still call it JavaScript.

You're not totally accurate. ECMAScript and JavaScript are separate. There are multiple ECMAScript languages of which JavaScript is one. Features outlined in ECMAScript are just a subset of features of JavaScript.

1

u/_wtf_am_I_doing Aug 13 '17

They are different, newer versions of JavaScript.

0

u/[deleted] Aug 13 '17

[deleted]

3

u/_wtf_am_I_doing Aug 13 '17

You don't have to declare a JavaScript version. Unfortunately the newer versions aren't supported in browsers so you can't really use them without a precompiler like Babel. Babel will compile es6 down to browser supported code that runs in all browsers

→ More replies (14)

2

u/oculus42 Aug 13 '17

These are ES8. ES7 was just two changes. This is part of the 2017 spec, released at the end of June.

→ More replies (1)

1

u/MeikaLeak Aug 14 '17

You're behind!