Projecting a promise in jQuery

In some cases, we have to just display the name of a promise. This will be much needed when you only want to see what the element can be or what operations you want to perform on an object. Using jQuery, we can easily achieve it by using the pipe() function.

Consider this code where we are projecting the result, which is an actor:

<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.0.js"></script> <script> var post = $.post("/echo/json/", { json: JSON.stringify({firstName: "Arnold", lastName: "Schwarzenegger"}) } ).pipe(function(p){ return "Name Saved >> " + p.firstName + " " + p.lastName; }); post.done(function(r){ alert(r); }); </script> </head> <body> <h1> you have seen the result . </h1> ...

Get Mastering JavaScript Promises 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.