Handling Failures with then

then accepts a second argument, which is invoked when the Deferred shifts to the rejected state. To see this in action, add a second function argument (making sure to add a comma between the two function arguments) to formHandler.addSubmitHandler in main.js. Inside of this function, show an alert with a simple error message.

...
  formHandler.addSubmitHandler(function (data) {
    myTruck.createOrder.call(myTruck, data)
      .then(function () {
        checkList.addRow.call(checkList, data);
      },
      function () {
        alert('Server unreachable. Try again later.');
      }
      );
  });
...

At the top of main.js, misspell the server name so that Ajax requests fail. (This change will only be temporary, so you may want to simply cut a section out ...

Get Front-End Web Development: The Big Nerd Ranch Guide 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.