nodejs - 函数返回不退出 [英] nodejs - Function Return Not Exiting

查看:250
本文介绍了nodejs - 函数返回不退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行一系列测试的函数.如果第一个测试失败,函数应该退出,但它不会:

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";
}

我得到Processed"的控制台输出,然后是Processing".我永远不应该在那里看到带有返回的处理".我在这里错过了什么?

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" 只是从回调函数返回,而不是从 checkSite() 所以它不影响接下来发生的事情.

return "Processed" is just returning from the callback function, not from checkSite() so it does not influence what happens next.

fs.readFile() 是异步的.如果您希望第二个 fs.readFile() 等待第一个完成,或者您不想在第一个 fs.readFile() 成功完成后执行进一步的操作,你将不得不以不同的方式编码.

fs.readFile() is asynchronous. 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.

这是一种方法:

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!');
                    });
                }
            });
        }
    });
}

注意:您不能直接从 checkSite() 返回成功或失败的结果,因为 fs.readFile() 操作是异步的,因此 checkSite() 在这些操作完成之前返回.如果你想将结果返回给 checkSite() 的调用者,那么你要么需要传入一个被结果调用的回调函数,使用 promise 的结构来帮助你或者切换到同步函数而不是异步版本(通常不推荐).

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. 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).

这篇关于nodejs - 函数返回不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆