Asynchronous code involving events

For asynchronous JavaScript APIs involving events, you need to register the success and error event handlers that will be executed depending on whether the operation was a success or failure respectively.

For example, while making an AJAX request, we register the event handlers that will be executed depending on whether the AJAX request was made successfully or not. Consider this code snippet, which makes an AJAX request and logs the retrieved information:

function displayName(json) {    try {        //we usally display it using DOM        console.log(json.Name);    } catch(e) {    console.log("Exception: " + e.message);    }}function displayProfession(json) {    try {        console.log(json.Profession);    } catch(e) { console.log("Exception: ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.