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 ...

Get Async JavaScript 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.