针对async.detect的Node.js'Not Found'回调?

时间:2021-04-04 19:44:11

I have a file path list file_paths, and I want to detect which file exists. If any file exists, I want to read that one. Otherwise call another function, not_found for example.

我有一个文件路径列表file_paths,我想检测哪个文件存在。如果存在任何文件,我想阅读那个文件。否则调用另一个函数,例如not_found。

I wish to use async.detect but I found no way to add a 'Not Found' callback when all the iterators return false.

我希望使用async.detect,但是当所有迭代器返回false时,我发现无法添加“未找到”回调。

I've tried this one, but no work. Returned undefined and nothing outputted.

我试过这个,但没有工作。返回undefined并没有输出。

async = require 'async'

async.detect [1,2,3], (item, callback) ->
  callback true if item == 4
, (result) ->
  console.log result ? result : 'Not Found'

If there's another way to do it, please add it to the answer.

如果还有其他方法,请将其添加到答案中。

2 个解决方案

#1


1  

from the documentation you mentioned.

从你提到的文件。

in case of detect(arr, iterator, callback)

在检测的情况下(arr,迭代器,回调)

callback(result) - A callback which is called as soon as any iterator returns true, or after all the iterator functions have finished. Result will be the first item in the array that passes the truth test (iterator) or the value undefined if none passed.

callback(result) - 任何迭代器返回true或所有迭代器函数完成后立即调用的回调函数。结果将是数组中传递真值测试(迭代器)的第一项,如果没有传递,则为undefined值。

from your question you want to find a way to detect if no file in list is found, which could be done by comparing the result with undefined and checking whether this condition is true.

从你的问题中你想找到一种方法来检测是否找不到列表中的文件,这可以通过将结果与undefined进行比较并检查这个条件是否为真来完成。

like

async.detect(['file1','file2','file3'], fs.exists, function(result){

     if(typeof(result)=="undefined") {
         //none of the files where found so undefined
     }

});

#2


0  

I would use async.each and use fs.exists to detect if the file exists. If it exists, then read the file otherwise, call the not found function then proceed to the next item. See sample code I have written on top of my head below.

我会使用async.each并使用fs.exists来检测文件是否存在。如果存在,则读取文件,否则调用未找到的函数,然后继续下一个项目。请参阅我在下面编写的示例代码。

async.each(file_paths, processItem, function(err) {
  if(err) {
    console.log(err);
    throw err;
    return;
  }

  console.log('done reading file paths..');

});

function notFound(file_path) {
  console.log(file_path + ' not found..');
}

function processItem(file_path, next) {
  fs.exists(file_path, function(exists) {
    if(exists) {
      //file exists
      //do some processing
      //call next when done
      fs.readFile(file_path, function (err, data) {
        if (err) throw err;

        //do what you want here

        //then call next
        next();
      });

    }
    else {
      //file does not exist
      notFound(file_path);
      //proceed to next item
      next();
    }
  });
}

#1


1  

from the documentation you mentioned.

从你提到的文件。

in case of detect(arr, iterator, callback)

在检测的情况下(arr,迭代器,回调)

callback(result) - A callback which is called as soon as any iterator returns true, or after all the iterator functions have finished. Result will be the first item in the array that passes the truth test (iterator) or the value undefined if none passed.

callback(result) - 任何迭代器返回true或所有迭代器函数完成后立即调用的回调函数。结果将是数组中传递真值测试(迭代器)的第一项,如果没有传递,则为undefined值。

from your question you want to find a way to detect if no file in list is found, which could be done by comparing the result with undefined and checking whether this condition is true.

从你的问题中你想找到一种方法来检测是否找不到列表中的文件,这可以通过将结果与undefined进行比较并检查这个条件是否为真来完成。

like

async.detect(['file1','file2','file3'], fs.exists, function(result){

     if(typeof(result)=="undefined") {
         //none of the files where found so undefined
     }

});

#2


0  

I would use async.each and use fs.exists to detect if the file exists. If it exists, then read the file otherwise, call the not found function then proceed to the next item. See sample code I have written on top of my head below.

我会使用async.each并使用fs.exists来检测文件是否存在。如果存在,则读取文件,否则调用未找到的函数,然后继续下一个项目。请参阅我在下面编写的示例代码。

async.each(file_paths, processItem, function(err) {
  if(err) {
    console.log(err);
    throw err;
    return;
  }

  console.log('done reading file paths..');

});

function notFound(file_path) {
  console.log(file_path + ' not found..');
}

function processItem(file_path, next) {
  fs.exists(file_path, function(exists) {
    if(exists) {
      //file exists
      //do some processing
      //call next when done
      fs.readFile(file_path, function (err, data) {
        if (err) throw err;

        //do what you want here

        //then call next
        next();
      });

    }
    else {
      //file does not exist
      notFound(file_path);
      //proceed to next item
      next();
    }
  });
}