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.