Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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,所以我试图在一个文件中运行一些检查。让我们假设我有 module.exports = async (content) => { // Check no.1 if (content.id != 'SomeID') return; // Check no.2 if (content.length > 20) return; //..etc } 在我的主文件中,我需要这个文件。我希望它返回到原始文件中,具体取决于checks.js的结果,因此假设内容id与“SomeID”不同,我希望它返

所以我试图在一个文件中运行一些检查。让我们假设我有

module.exports = async (content) => {
// Check no.1
if (content.id != 'SomeID') return;
// Check no.2
if (content.length > 20) return;
//..etc
}

在我的主文件中,我需要这个文件。我希望它返回到原始文件中,具体取决于
checks.js
的结果,因此假设内容id与“SomeID”不同,我希望它返回,而不是继续主文件中的其余代码。我确实
require('filePath')(content)
但它实际上并没有像
checks.js中的说明那样返回到主文件中。提前谢谢你

checks.js
正在返回一个
异步函数,您必须等待它

checks.js:

module.exports = async (content) => {
  // Check no.1
  if (content.id != 'SomeID') return;
  // Check no.2
  if (content.length > 20) return;
  //..etc

  return true // maybe your not returning truthy?
}
const checks = require('./checks');

(async () => {

  console.log('typeof checks()', typeof checks);
  console.log('instance of', checks.constructor.name);

  //
  let content = {
    id: 'SomeID'
  };

  if (await checks(content)) {
    console.log('1. passed');
  } else {
    console.log('1. failed');
  }

  //
  content = {
    id: 'WrongID'
  };

  if (await checks(content)) {
    console.log('2. passed');
  } else {
    console.log('2. failed');
  }
})();

index.js:

module.exports = async (content) => {
  // Check no.1
  if (content.id != 'SomeID') return;
  // Check no.2
  if (content.length > 20) return;
  //..etc

  return true // maybe your not returning truthy?
}
const checks = require('./checks');

(async () => {

  console.log('typeof checks()', typeof checks);
  console.log('instance of', checks.constructor.name);

  //
  let content = {
    id: 'SomeID'
  };

  if (await checks(content)) {
    console.log('1. passed');
  } else {
    console.log('1. failed');
  }

  //
  content = {
    id: 'WrongID'
  };

  if (await checks(content)) {
    console.log('2. passed');
  } else {
    console.log('2. failed');
  }
})();

运行时将输出:

typeof checks() function
instance of AsyncFunction
1. passed
2. failed
 

有关更多详细信息,请参阅。

its,因为您已将其定义为异步,所以至少需要
wait(require('filePath'))(content)
,尽管不内联很难看,但这并不能解决问题。你能解释一下你答案背后的逻辑吗?只是为了让我更受教育。你说它实际上没有像应该的那样返回主文件是什么意思?什么是
文件路径
,可能应该是
/检查
请显示您如何使用该文件。如前所述,不需要(
require('filePath')(content)
然后您就可以调试它了。而且您的代码不需要异步,您是否检查了
content
实际上是什么?加上。。。如果内容可以是对象或字符串/数组,则在使用typeof进行现有检查之前,您应该先进行检查,否则如果传入字符串,则第一次检查将出错。我知道如何要求文件lmao。这是一个简单的
require('./checks')(content)
。我的意思是它不会返回:如果我将id和长度检查放在上面的问题的主文件中,它会返回,这意味着它不会继续处理其余的代码,但是当我需要
检查
文件而不是直接放入时,它不会返回,它继续处理主文件中的其余代码,这不是我想要的。我希望它在
checks.js
中运行检查,如果检查告诉它返回,它将不会运行主文件代码的其余部分。希望我说清楚了。