re: How To Avoid Javascript Bugs VIEW POST

FULL DISCUSSION
 

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.

😄

 
code of conduct - report abuse