Quantcast
Channel: How do I return the response from an asynchronous call? - Stack Overflow
Viewing all articles
Browse latest Browse all 46

Answer by jsbisht for How do I return the response from an asynchronous call?

$
0
0

Another approach to return a value from an asynchronous function, is to pass in an object that will store the result from the asynchronous function.

Here is an example of the same:

var async = require("async");// This wires up result back to the callervar result = {};var asyncTasks = [];asyncTasks.push(function(_callback){    // some asynchronous operation    $.ajax({        url: '...',        success: function(response) {            result.response = response;            _callback();        }    });});async.parallel(asyncTasks, function(){    // result is available after performing asynchronous operation    console.log(result)    console.log('Done');});

I am using the result object to store the value during the asynchronous operation. This allows the result be available even after the asynchronous job.

I use this approach a lot. I would be interested to know how well this approach works where wiring the result back through consecutive modules is involved.


Viewing all articles
Browse latest Browse all 46

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>