DEV Community

Discussion on: 5 easy wins for cleaner Javascript code ๐Ÿงน

Collapse
 
jamesthomson profile image
James Thomson

Also, use Promises properly. Don't nest Promises, use the chain to call other functions.

e.g.

function getCall () {
  return new Promise((res, rej) => setTimeout(() => res('success'), 2000))
}

function doSomething () {
  console.log('do something else')
}

getCall()
  .then(res => console.log(res))
  .then(doSomething)
  .then(() => console.log('done'))

jsfiddle.net/jamesbrndwgn/4sxty91n/1/

Also, make use of async/await.

Collapse
 
mlevkov profile image
Mikhail Levkovsky

For sure! I have a whole other article coming up on how to clean up promises :)
Thanks for sharing