Skip to Main Content
Async JavaScript
book

Async JavaScript

by Trevor Burnham
November 2012
Intermediate to advanced content levelIntermediate to advanced
106 pages
2h 20m
English
Pragmatic Bookshelf
Content preview from Async JavaScript

Handling Async Errors

Like many modern languages, JavaScript allows you to throw exceptions and catch them in a try/catch block. If uncaught, most environments will give you a helpful stack trace. For example, this code will throw an exception because ’{’ is invalid JSON:

EventModel/stackTrace.js
 
function​ JSONToObject(jsonStr) {
 
return​ JSON.parse(jsonStr);
 
}
 
var​ obj = JSONToObject(​'{'​);
<= 
SyntaxError: Unexpected end of input
 
at Object.parse (native)
 
at JSONToObject (/AsyncJS/stackTrace.js:2:15)
 
at Object.<anonymous> (/AsyncJS/stackTrace.js:4:11)

The stack trace tells us not only where the error was thrown from but also where the original mistake was made: line 4. Unfortunately, tracking down the causes of async errors isn’t ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Modern Asynchronous JavaScript

Modern Asynchronous JavaScript

Faraz K. Kelhini
Learn TypeScript 3 by Building Web Applications

Learn TypeScript 3 by Building Web Applications

Sebastien Dubois, Alexis Georges

Publisher Resources

ISBN: 9781941222782Errata