Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think a nice way might be to call your evaluate function recursively until all computations are complete. Use a queue for your computations and process them in a FIFO manner. </p> <pre><code>var computations = []; </code></pre> <p>You would need to ditch the n.once('message' and outside your evalutate function define a n.on('message' handler. With each 'message' you receive that matches your evaluation data type (let's call it 'result'). Then you'd save that result in a FIFO queue of results. After which, you'd check if there are more computations, and call evaluate again. This can be cleaned up to splice off the oldest computation in the evaluate function if you'd like.</p> <pre><code>var results = []; n.on('message', function(m) { if(m.msg === "result") { // message looks like {msg: "result", data: 1234} results.push(m.data); } if(computations.length &gt; 0) { var comp = computations[0]; // Save oldest computation computations.splice(0,1); // Remove oldest computation from array evaluate(comp); // Evaluate oldest computation } else process_results(); // if you have 0 computations left, // you got the last result so go process them. }); </code></pre> <p>Before whatever is calling evaluate, you should push your computation into the queue, and send the oldest to evaluate.</p> <pre><code>if(computations.length &gt; 0) { computations.push(new_comp); // Push on your newest compuation var comp = computations[0]; // Save oldest computation computations.splice(0,1); // Remove oldest computation from array evaluate(comp); // Evaluate oldest computation } else { evaluate(new_comp); // Evaluate computation } evaluate(new_comp); // Evaluate oldest computation </code></pre> <p>Your "DONE" Flag will be when computations.length === 0. And that will just naturally take care of itself with the above code. Any time you have 0 computations left in the n.on('message', handler.. you can call your results processing function:</p> <p>To process your results it's a simple for loop.</p> <pre><code>function process_results() { if(results.length &gt; 0) for(x in results) console.log("New result: "+x); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload