I used events in nodejs to implement my logic. And "nextParams" is an event that make function "extractVideo" executed again. I know I used recursive event loop. I tried to used setTimeout(function, 0) and setTimeout(function, 1) in my code, but the error "Maximum call stack size exceeded" still appear, I wonder does anybody know how to fix this problem, I am appreciated very much!
videoCrawler.prototype.extractVideo=function(queryStr){
self.on("videoIdArray",function(){
if(videoIdArray.length){
self.getVideoInfo(videoIdArray.pop());
}else{
self.videomonitor.emit("nextParams");
}
});
self.on("getVideoIdArray",function(videoidarray){
if(!(videoidarray || videoidarray.length)){
self.videomonitor.emit("nextParams");
}
videoIdArray=videoidarray;
self.getVideoInfo(videoIdArray.pop());
});
self.on("save",function(videodetailinfo){
if(!videodetailinfo){
if(videoIdArray.length){
self.getVideoInfo(videoIdArray.pop());
}else{
self.videomonitor.emit("nextParams");
}
}
self.saveVideoInfo(videodetailinfo);
});
self.on("errorCapture",function(errInfo){
if(errInfo.message=="queryStr is not exits"){
setTimeout(self.videomonitor.emit("nextParams"),1);
}else if(errInfo.message=="videodetailsinfo is not exits"){
self.getVideoInfo(videoIdArray.pop());
}else if(errInfo.message=="videoId is empty"){
self.getVideoInfo(videoIdArray.pop());
}else if(errInfo.message=="save fails"){
self.getVideoInfo(videoIdArray.pop());
}
});
self.getVideoId(queryStr);
}
below are functions
videoCrawler.prototype.getVideoId=function(queryStr){
if(!queryStr) this.emit("errorCapture",errInfo);
//dosomething
this.emit("getVideoIdArray",videoArray);
}
videoCrawler.prototype.getVideoInfo=function(videoId){
if(!queryStr) this.emit("errorCapture",errInfo);
//dosomething
self.emit("save",videodetails);
}
videoCrawler.prototype.saveVideoInfo=function(videodetailsinfo){
if(!queryStr) this.emit("errorCapture",errInfo);
//dosomething
self.emit("videoIdArray");
}