Using Deferreds with Callback-Only APIs

Sometimes you will need to coordinate your Deferred-based code with callback-only APIs, such as event listeners.

Currently, formHandler.addSubmitHandler resets the form and focuses on the first element – no matter what happens with the Ajax request. However, you only want those things to happen if the Ajax request is successful. Put another way, you only want those things to happen if the Deferred is fulfilled.

How can you know whether the Deferred is fulfilled? Your function argument to addSubmitHandler can return the Deferred, and inside of addSubmitHandler you can chain a .then call to the Deferred.

In main.js, add a return keyword to the callback.

... formHandler.addSubmitHandler(function ...

Get Front-End Web Development: The Big Nerd Ranch Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.