Passing Data to Callbacks
A Promise can give its callbacks additional information. For example, these two Ajax snippets are equivalent:
â | â// Using a callback directlyâ |
â | $.get(url, successCallback); |
â | â// Binding a callback to a Promiseâ |
â | âvarâ fetchingData = $.get(url); |
â | fetchingData.done(successCallback); |
When you resolve or reject a Deferred, any arguments you provide are relayed to the corresponding callbacks.
â | âvarâ aDreamDeferred = ânewâ $.Deferred(); |
â | aDreamDeferred.done(âfunctionâ(subject) { |
â | console.log(â'I had the most wonderful dream about'â, subject); |
â | }); |
â | aDreamDeferred.resolve(â'the JS event model'â); |
â<=Â | I had the most wonderful dream about the JS event model |
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.