calling asynchronous functions only once completed in callback nodejs
<p>i have code which somehow receives 100000 datasets.
and then there is a storage that has to be accessed in a way that only once the last addition has been finished the next one can start.</p>
<p>in a syncronous way that would look like this .. so the add method would block .</p>
<pre><code>var data = [...]; //100000 datasets 
var syncstorage = require( 'syncstorage' ); // syncronous storage.

for( var i = 0 ; i &lt; data.length() ; i++ ) {
    syncstorage.add( data[i] ); // will only return once stored
}
</code></pre>
<p>the async storage does not block however it will tell you when it is done by a callback ...</p>
<pre><code>/////// async storage 

asyncstorage.add( data[i] , function(err) { /* can only execute another add once i get this response */ } )
</code></pre>
<p>i only came up with this now :</p>
<pre><code>var i = 0;
function execute() {
    if( i &gt;= data.length()){
        return;
    }
    asyncstorage.add( data[i] , function(err) { i++; execute(); } )     
}
</code></pre>
<p>however it would result in an extreme callstack</p>
<p>maybe i would need an event emitter and emit it in that callback?
kinda a resource wrapper?
how is this resolved? ... i did sadly not find results in stack overflow concerning this specific issue;</p>
<h2>ruled out solutions</h2>
<ul>
<li>async.each
<ul>
<li><a href="http://caolan.github.io/async/docs.html#.each" rel="nofollow noreferrer">http://caolan.github.io/async/docs.html#.each</a></li>
<li>&quot;this function applies iteratee to each item in parallel&quot;</li>
</ul>
</li>
<li>async.series
<ul>
<li><a href="http://caolan.github.io/async/docs.html#.series" rel="nofollow noreferrer">http://caolan.github.io/async/docs.html#.series</a></li>
<li>
<ol>
<li>it requires an array of functions not of data</li>
</ol>
</li>
<li>
<ol start="2">
<li>it would require the callback to be called from the callback to not also be run quasiparallel</li>
</ol>
</li>
<li>so it could be used but only with disproportionately memory requirements (functions)</li>
<li>also it is not sure how async handles this internally (call stack wise)</li>
</ul>
</li>
</ul>
<h2>test example</h2>
<pre><code>var store = {add:function(d,cb){cb(null)}};
var d=[]; for(var i = 0 ; i &lt; 100000; i ++) { d.push(i)}; d;
var async = require('async');
async.eachSeries(d,store.add);
</code></pre>
<p><strong>does not work!</strong>
this is, because async assumes that there will be an event emitter present in the iteratee function.
therefore a simple test class like the above runs in a <code>Maximum call stack size exceeded</code></p>