O'Reilly logo

Async JavaScript by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Binding to the Future with pipe

A big reason why performing a series of async tasks is often inconvenient in JavaScript is that you can’t attach handlers to the second task until the first one is complete. As an example, let’s GET data from one URL and then POST it to another.

 
var​ getPromise = $.get(​'/query'​);
 
getPromise.done(​function​(data) {
 
var​ postPromise = $.post(​'/search'​, data);
 
});
 
// Now we'd like to attach handlers to postPromise...

Do you see what the problem is here? We can’t bind callbacks to postPromise until our GET operation is done, because it doesn’t exist yet! It’s created by a $.post call that we can’t make until we have the data that we’re getting asynchronously from the $.get call.

That’s why jQuery ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required