-4

Im newbie in nodejs, so I have a simple problem, but can't solve it.

For example, I have func like this:

var func = function(){  
  setTimeout(function(){  
    return 5;  
  }, 1000);  
}; 

when I call func i got 'undefined'. I understand why, but I can't understand how I can change my function for returning 5 instead of undefined.
I can rewrite func with callback argument, and call callback from setTimeout, but I want to return result from function without using callbacks.
In V8 we have generators and keyword 'yield', I think it maybe help me,so can anyone explain how it works and how to use it in this example. Thanks.

Tom Sawyer
  • 66
  • 1
  • 6
  • 1
    The notion of getting a return value from an asynchronous callback is fundamentally nonsensical. In Node, you have to think *asynchronous* for just about everything in your software design. That's why Node APIs all involve callbacks, and yours will too. – Pointy Dec 06 '15 at 15:52
  • OK, I understand you, look, I'll try to explain my problem. I gave a simple example in question, but in fact the problem is this: I have a certain class. it has a function that does something asynchronously. I have an array of instances of this class, and I want to get an array with the results of these functions. If I write this: array.map((e) => {e.func()})); than i get array filled with 'null', so give me piece of code that will work. Async.map will do this, but how? i cant understand – Tom Sawyer Dec 06 '15 at 15:59
  • 1
    "so give me piece of code that will work"... did you really just say that? – An0nC0d3r Dec 06 '15 at 16:03
  • 2
    Using promises is one way to deal with your issue: http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 Look for `Promise.all()`. – Shanoor Dec 06 '15 at 16:03
  • 1
    If you want to stem the tide of downvotes, then take the ACTUAL description of your problem from your previous comment and edit it into your question so we know what the real problem to be solved here is. The best questions here describe their actual problem, not the issues with their attempted solution. And, give up the demanding attitude about "give me code". You're just talking to regular people here. We are more likely to help if you are considerate, describe your problem well and ask for help rather than demand help. – jfriend00 Dec 06 '15 at 16:12

1 Answers1

1

OK, here's my understanding of your problem statement (which you should have put in your question in the first place).

You have an array of instances of an object that has an async method that eventually produces some value. And, you want to call each object in your array and produce an array of their async results.

There are several ways to approach that. I'll first show one with Promises.

// your object
function MyObj() {
}

// with an async method that returns a promise
MyObj.prototype.doAsync = function() {
    return new Promise(function(resolve) {
        setTimeout(function() {
            // create some value to resolve with here 
            // I've just substituted a random value
            resolve(Math.floor(Math.random() * 1000));
        }, 50);
    });
}

// make an array of these objects like your problem statement described
var arr = [];
for (var i = 0; i < 20; i++) {
    arr[i] = new MyObj();
}  

// collect an array of promises by running all the async methods
var promises = [];
for (i = 0; i < arr.length; i++) {
   promises.push(arr[i].doAsync());
}

// wait for all the promises to be done
Promise.all(promises).then(function(results) {
    // array of async results here
    log(results);
});


function log(x) {
    var div = document.createElement("div");
    div.innerHTML = JSON.stringify(x);
    document.body.appendChild(div);
}

For a general reference on returning data from an async function, see How do I return the response from an asynchronous call?

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979