Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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 操作完成前返回Nodejs函数_Javascript_Node.js_Asynchronous - Fatal编程技术网

Javascript 操作完成前返回Nodejs函数

Javascript 操作完成前返回Nodejs函数,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我有一个关于我的Nodejs程序的问题。我有一个FindItemFromUrlList函数,它在for循环中调用另一个TestUrl。我希望FindItemFromUrlList在测试完所有URL后返回找到的URL(这样列表就会找到URL)。但是,当遍历for循环,并且URL被测试并找到时,当我执行console.log(foundUrls)时,在TestUrl中有一个空列表。我可以看到URL已经被找到,并且我假设该算法在for循环之前执行了console.log函数,我尝试了一个。然后它就不起

我有一个关于我的Nodejs程序的问题。我有一个FindItemFromUrlList函数,它在for循环中调用另一个TestUrl。我希望FindItemFromUrlList在测试完所有URL后返回找到的URL(这样列表就会找到URL)。但是,当遍历for循环,并且URL被测试并找到时,当我执行console.log(foundUrls)时,在TestUrl中有一个空列表。我可以看到URL已经被找到,并且我假设该算法在for循环之前执行了console.log函数,我尝试了一个。然后它就不起作用了

function FindItemFromUrlList(KeyworList, UrlList, Color, Excludedurls){
    var completed_requests = 0 ;
    var itemfound = 0;
    foundUrls = [];
    for(urls of UrlList){
        completed_requests++;
        TestUrl(KeyworList, urls, itemfound, completed_requests, foundUrls, UrlList, Color, Excludedurls);
    }
    console.log(foundUrls);
}

function TestUrl(KeyworList, urls, itemfound, completed_requests, foundUrls, UrlList, Color, Excludedurls){
    https.get(urls, function (res) {
        var htmlcode2 = "";
        res.on('data', (d) => {
            htmlcode2 += d;
        })
            .on('end', () => {
                for (keyword of KeyworList) {
                    if (htmlcode2.toString().includes(keyword) && !foundUrls.includes(urls) && (!(Excludedurls && urls.includes(Excludedurls))) ){
                        foundUrls.push(urls);
                    }
                }
                if (completed_requests == UrlList.length) {
                    // All download done, process responses
                    console.log("Done all requests");
                    console.log("nombre d'items trouvés : ",foundUrls.length);
                    console.log("Url trouvées :", foundUrls);
                }
            })})
}
以下是我的代码运行的结果:

[]
Done all requests
nombre d'items trouvés :  5
Url trouvées : [
  'https://www.supremenewyork.com/shop/tops-sweaters/jmqsjoxe5/iuawc8jf5',
  'https://www.supremenewyork.com/shop/tops-sweaters/jmqsjoxe5/if60rct4l',
  'https://www.supremenewyork.com/shop/tops-sweaters/jmqsjoxe5/dgkjtbima',
  'https://www.supremenewyork.com/shop/tops-sweaters/jmqsjoxe5/wwx6gdai0',
  'https://www.supremenewyork.com/shop/tops-sweaters/jmqsjoxe5/x2y3lk890'
]
您的
https.get(…)
是异步的,并接收回调,这意味着您的
console.log(foundUrls)
将在
TestUrl
func完成并填充
foundUrls
数组之前执行

异步函数FindItemFromUrlList(KeyworList、UrlList、Color、Excludedurls){ var已完成的_请求=0; var itemfound=0; 常量承诺=[] foundURL=[]; 用于(UrlList的URL){ 已完成的_请求++; const promises=TestUrl(KeyworList、url、itemfund、已完成的_请求、foundurl、urlist、Color、Excludedurls); 承诺。推动(承诺) } 等待承诺。所有(承诺)//这将等待承诺解决 console.log(foundurl); } 异步函数TestUrl(KeyworList、URL、itemfound、已完成的\u请求、foundUrls、, URL列表,颜色,不包括URL){ 返回新承诺((解决、拒绝)=>{ https.get(URL、函数(res){ var htmlcode2=“”; res.on('数据',(d)=>{ htmlcode2+=d; }) .on('end',()=>{ for(关键字列表的关键字){ if(htmlcode2.toString().includes(关键字)和&&!foundUrls.includes(URL)和&(!(Excludedurls和&urls.includes(Excludedurls))){ 推送(URL); } } resolve()//在这里您可以解析承诺 if(已完成的_请求==urlist.length){ //所有下载完成,处理响应 console.log(“完成所有请求”); log(“nombre d'items trouvés:”,foundUrls.length); log(“Url trouvées:”,foundurl); } })}) }) } 您可以使用async关键字和承诺来处理此问题。使用
等待承诺。所有(…)
将等待所有承诺完成后再继续。 如果你以前从未使用过承诺,我建议你读下一篇


编辑:添加代码和说明

您好。你能在你的帖子里写上你的代码吗。这使得这里的人更容易帮助你解决问题。如果您不确定如何正确格式化,在您编辑问题后,这里的某人会为您这样做,使您的函数异步,以便它可以等待响应是的,我看到了,但如何在for循环之后执行此console.logfinished@BarnabeMugnier嘿,我编辑了我的答案。我添加了我认为可以解决您问题的代码。