DEV Community

How To Avoid Javascript Bugs

Mainasara Al-amin Tsowa on April 04, 2019

Being a javascript programmer is great but we can never avoid the dreaded stack trace! 😡, most of these bugs are just one Google search away from b...
Collapse
 
gypsydave5 profile image
David Wickes • Edited

Correct usage.

let laterUsed;

doAsyncTask().then(()=>{
    useVariable(laterUsed || externalAlternativeValue); 
    // ✅ Another value is used when the original value is undefined
})

hmmm ...

Even More Correct Usage

doAsyncTask()
  .then(result => {
    useVariable(laterUsed)
  })
  .catch(e => {
    // handle whatever happens when doAsyncTask() goes wrong
  })

If you're trying to avoid bugs in JavaScript, I'd really recommend

  • not communicating between asynchronous processes using global variables.
  • not using undefined to indicate an error state. Or an absence of value.

😄

Collapse
 
neutrino2211 profile image
Mainasara Al-amin Tsowa

Absolutely correct! 😃, may I add it in the post?

Collapse
 
gypsydave5 profile image
David Wickes

Please do! 👍😁

Collapse
 
neutrino2211 profile image
Mainasara Al-amin Tsowa

100% true, may I add it to the conclusion?