Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理大型数据集时JavaScript堆栈溢出_Javascript_Node.js - Fatal编程技术网

处理大型数据集时JavaScript堆栈溢出

处理大型数据集时JavaScript堆栈溢出,javascript,node.js,Javascript,Node.js,我刚刚创建了一个小的NodeJS应用程序,用于执行一些网络IO,但由于回调的数量太多,遇到了我认为是堆栈溢出的情况 我的应用程序基本上是一个由2个网络操作组成的文件传输函数。。。让我们调用它们fetchData()和sendData()。他们所做的只是一些获取或发布数据的HTTP请求 我的应用程序设置如下: function runTransfer(){ // I need to transfer 100 million small documents // So I nee

我刚刚创建了一个小的NodeJS应用程序,用于执行一些网络IO,但由于回调的数量太多,遇到了我认为是堆栈溢出的情况

我的应用程序基本上是一个由2个网络操作组成的文件传输函数。。。让我们调用它们
fetchData()
sendData()
。他们所做的只是一些获取或发布数据的HTTP请求

我的应用程序设置如下:

function runTransfer(){
     // I need to transfer 100 million small documents
     // So I need to fetch and send a group (1000) at a time
     fetchData(fetchDataCallback);


    function fetchDataCallback(data){
         // Now that I have my data, lets send it to my other 'store'
         sendData(data, sendDataCallback);
     }

    function sendDataCallback(){
       // now that I got a 200 OK response we can fetch more data... and so on
       if(totalDocsFetched >= totalNumDocs){
             return; // This is where the application would finally end, once we fetched and sent all 100 million docs
       }
       fetchData(fetchDataCallback);


    }


}
按照这种设计模式,我认为最终会出现堆栈溢出,因为
fetchData->fetchDataCallback->sendData->sendDataCallback->fetchData->fetchDataCallback->sendData->sendData…
等等,直到我的堆栈爆炸


我可以在这里使用什么样的设计模式来确保不会出现这样的溢出?

如果操作是异步的,那么您就不能有堆栈溢出,因为在
sendDataCallback
运行之前很久,前面的
fetchDataCallback
就会返回。如果我的回调是匿名函数,那么异步回调链中就不能有堆栈溢出,可能会因为同一fn有数千个定义而导致某种类型的溢出吗?不,如果它们是异步的,则不会导致溢出或任何类型的资源问题,因为在清除callstack之前,下一次回调都不会发生。hmmmm。。这很奇怪,因为在运行命令时,我的整个机器崩溃并重新启动。我被ssh连接到机器中,没有运行“top”,所以我不确定是内存还是什么原因导致了它。