O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dealing with errors

Detecting and treating errors in asynchronous code are hard tasks. For synchronous code, we can use the famous try/catch/finally block. Treating an exception with this block is easy, as can be seen in the following code:

try{   throw new Error('An error occurred'); }catch(err){   console.log('Treating the error'); }finally{   console.log('Last computation'); } 

Unfortunately, we can't use the try/catch/finally on asynchronous code. In JavaScript we run asynchronous code most of the time, so the language and the community elaborated a couple of strategies.

Initially, the only strategy we had available to deal with asynchronous code was the use of callback functions, so most libraries in JavaScript added the onSuccess() and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required