简体   繁体   English

nodejs-函数返回未退出

[英]nodejs - Function Return Not Exiting

I have a function that does a series of tests. 我有一个功能,可以进行一系列测试。 If the first test fails the function should exit, however it doesn't: 如果第一个测试失败,则该函数应退出,但是不会退出:

function checkSite(site) {
console.log("Checking if site is already processed or processing");
// Processed Sites
fs.readFile("processed.txt", "utf8", function(error, data) {
 if (data.indexOf(site) > -1) {
  console.log("Processed");
  return "Processed";
 } else {
  console.log("Not Processed");
 }
});
fs.readFile("processing.txt", "utf8", function(error, data) {
if (data.indexOf(site) > -1) {
 console.log("Processing");
 return "Processing";
} else {
 console.log("Not Processing, Queue for Processing");
 // Queue for Processing
 fs.appendFile(fileProcessing, site, function (err) {
  if (err) throw err;
  console.log('The "data to append" was appended to file!');
 });
}
});
return "Success";
}

I'm getting console output of "Processed" followed by "Processing". 我正在获取控制台输出“已处理”,然后是“正在处理”。 I should never see "Processing" with the return there. 我永远都不会看到“处理中”的返回信息。 What am I missing here? 我在这里想念什么?

return "Processed" is just returning from the callback function, not from checkSite() so it does not influence what happens next. return "Processed"只是从回调函数返回,而不是从checkSite()返回,因此它不会影响接下来发生的事情。

fs.readFile() is asynchronous. fs.readFile()是异步的。 If you want the second fs.readFile() to wait for the first to complete or you want to not execute further actions after the first fs.readFile() finishes successfully, you will have to code this differently. 如果您希望第二个fs.readFile()等待第一个fs.readFile()成功完成,或者您不想在第一个fs.readFile()成功完成后不执行其他操作,则必须使用不同的代码进行编码。

Here's one way to do it: 这是一种实现方法:

function checkSite(site) {
    console.log("Checking if site is already processed or processing");
    // Processed Sites
    fs.readFile("processed.txt", "utf8", function(error, data) {
        if (data.indexOf(site) > -1) {
            console.log("Processed");
        } else {
            console.log("Not Processed");
            fs.readFile("processing.txt", "utf8", function(error, data) {
                if (data.indexOf(site) > -1) {
                    console.log("Processing");
                } else {
                    console.log("Not Processing, Queue for Processing");
                    // Queue for Processing
                    fs.appendFile(fileProcessing, site, function(err) {
                        if (err) throw err;
                        console.log('The "data to append" was appended to file!');
                    });
                }
            });
        }
    });
}

Note: you can't return the success or failure result directly from checkSite() because the fs.readFile() operations are asynchronous and thus checkSite() returns before those operations are done. 注意:您不能直接从checkSite()返回成功或失败结果,因为fs.readFile()操作是异步的,因此checkSite()在完成这些操作之前会返回。 If you want to communicate the result back to the caller of checkSite() then, you will either need to pass in a callback function that gets called with the result, use the structure of promises to help you with that or switch to synchronous functions instead of async versions (not usually recommended). 如果您想将结果传达给checkSite()的调用者,那么您要么需要传递一个使用结果调用的回调函数,要么使用promises的结构来帮助您,或者切换到同步函数异步版本(通常不推荐)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM