Skip to content
loading...

Modern JavaScript, 10 things you should be using, starting today

Chris Noring on July 03, 2019

Follow me on Twitter, happy to take your suggestions on topics or improvements /Chris You may be completely new to JavaScript or you may have onl... [Read Full]
markdown guide
 

My problem with async await is it seems to reduce options for error handling. Any way to mitigate that?

 

hi. Yes you can have a try/catch inside of your async function, you can also have an error callback on the promise that invoking the async method will result in. I'll update the example, appreciate you raising this issue :)

 

Thanks. I'd love to see how handling async/await errors can be done in a way that isn't just as complicated as then/catch

you can do

shop.getFreeKittens().then(kittens=>console.log(kittens), err=>{
  // do your thing
  console.error(err);
});

yes, definitely that too. In my example that would be a catch-all handler. question I think was how to handle per call . So in promises it would be:

getUser()
  .then(getOrderByUser, errorHandler)
  .then(getOrderItemsByOrder, errorHandler)

The above scenario is what I think we are discussing.

For an error handling approach for async/await, we could do this:

async function get() {
  let user, order;
  try {
    user = await getUser();
  } catch(err) {}

 try {
  order = await getOrderByUser(user);
 } catch(err) {

 }
...
}

get()
  .then(successHandler, errorHandler)

I get the feeling that people can feel a bit intimidated when they first see a longer promise chain. this is were I believe async/await come in to their own, When used properly async functions can look pretty simple and straight forward. I still prefer promise style functions for most async tasks tho. Especially task like my example above.

 

Can you return the items variable from outside the try{} block? Or would it be scoped inside the try block.

Yes you can, but if you return any value from a async function, the the promise that function returns will resolve, successfully.even if in the catch block of a try/catch block. This behavior can be vary useful. If you want the promise to throw an error you need to call

throw expression

Okay, are you saying that a try catch block is not even necessary? Because the results of the error will get shoved into whatever is returned by the async function either way?

 

When you work with async/await you have the option to use catch as well.

For example:

await someAsync().catch(console.error);
 

Use try catch

try {
     await UserService.create(name, email);
} catch(error) {
    // Handle error
}
 
export default (a,b) => return a *b;

You'd need curly braces to be able to use return here, or just get rid of it.

const math = {
  add(a,b) => a + b
  sub(a,b) => a - b 
  multiply(a,b) => a *b
}

This syntax isn't valid. You cannot use arrow functions with the shorthand notation.

 
 

Nice rundown of our new ES6 sugar :)
On the array remainder example I'm guessing there is a little typo:

const array = [1,2,3,4,5,6];
const [a, ,c, ...remaining];

I'm guessing should be

const array = [1,2,3,4,5,6];
const [a, ,c, ...remaining] = array;

Tack och hej :)

 

Hi, I'd like to translate this nice article to Chinese. Can you give me the permission? The translated text will be published at nextfe.com and there will be backlink to this original post at the beginning. Thanks.

 

hi Jang sure. Please post here with a link to the chinese version once done, thanks :)

 
 

Hey, there's a typo on the first example of destructuring, I think you meant:

const name = req.body.name

Btw, nice article!!!

 
 
 

Very nice article, thank you. In 10. Arrow functions, the first example for printArray: can you really omit the parameter arr?

 

hi Mario, no that's my mistake, I'll fix it, thanks :)

 

I think this features should be mandatory in any JavaScript project. With the exception of some projects in Node.js where there's no ES6 modules.

 

agree.. they make the code smaller, more efficient, readable.. Hopefully, more people will use these features seeing this article. That was the goal :)

 

Totally agree. Great article, I know will help a lot of people :)

 

This is very useful and a great refresher on ES6. I really like this post!

 

Two typos I found randomly:

Section 8: So know we can do things...

Section 9: this is publically available

 

actually publically and publicly is too correct spellings.. appreciate you pointing it out Mike :)

code of conduct - report abuse