2016-11-19 5 views
2

次へthis snippet私は、トラフルをディレクトリに移動し、ディレクトリを検索し、それらのディレクトリからxmlファイル名を読み込む関数を記述しようとしています(フォルダ構造は、同じ)。これまでのところ、私の関数は期待どおりに動作していますが、関数から戻り値を取得しようとすると、私はPromiseオブジェクトを取得します。bluebird - 関数は実際のデータの代わりに約束のオブジェクトを返します

マイコード:

const Promise = require('bluebird'); 
const fs = Promise.promisifyAll(require('fs')); 
const path = require('path'); 

function getFileNames(rootPath) { 
    // Read content of path 
    return fs.readdirAsync(rootPath) 
    // For every file in path 
    .then(function(directories) { 
     // Filter out the directories 
     return directories.filter(function(file) { 
     return fs.statSync(path.join(rootPath, file)).isDirectory(); 
     }); 
    }) 
    // For every directory 
    .then(function(directories) { 
     return directories.map(function(directory) { 
     // Read file in the directory 
     return fs.readdirAsync(path.join(rootPath, directory)) 
      .filter(function(file) { 
      return path.extname(file) == '.XML'; 
      }) 
      .then(function(files) { 
      // Do something with the files 
      return files; 
      }); 
     }); 
    }); 
} 

getFileNames('./XML').then(function(files) { 
    console.log(files); 
}); 

I getFileNames内部の最後.then関数内console.log(files)、私はコンソール内のファイル名の実際の配列を取得します。しかし、上記のコードを実行すると、次の出力が得られます。

[ Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined }, 
    Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined }, 
    Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined }, 
    Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined }, 
    Promise { 
    _bitField: 0, 
    _fulfillmentHandler0: undefined, 
    _rejectionHandler0: undefined, 
    _promise0: undefined, 
    _receiver0: undefined } ] 

どうしてこの問題が発生し、修正するのですか?

答えて

2
ライン

.then(function(directories) { 
    return directories.map(function(directory) { 
    return fs.readdirAsync… 

お約束の配列のための約束を作成している、そしてそれはあなたがあなたの最後のログに取得している正確に何で

。代わりに、約束の配列を返すには、値の配列のための約束を返す必要がある - とPromise.allはないというまさにです:

.then(function(directories) { 
    return Promise.all(directories.map(function(directory) { 
    return fs.readdirAsync(…) 
    … 
    })); 
}) 

しかし、ブルーバード、使用することがより慣用だろうPromise.map(directories, function(…) { … })あるいはthe map method (すでに.filterは、各ディレクトリ内のファイルでやった使用方法に類似):

function getFileNames(rootPath) { 
    return fs.readdirAsync(rootPath) 
    .filter(function(file) { 
    return fs.statAsync(path.join(rootPath, file)).then(function(s) { 
     return s.isDirectory(); 
    }); 
    }) 
    .map(function(directory) { 
//^^^^ 
    return fs.readdirAsync(path.join(rootPath, directory)) 
    .filter(function(file) { 
     return path.extname(file) == '.XML'; 
    }) 
    .map(function(file) { 
     // Do something with every file 
     return file; 
    }); 
    }); 
} 
0

この

return getFileNames('./XML').then(function(files) { 
    console.log(files); 
    return files; 
}); 
+0

同じ出力を得ている –

+0

@MihaŠušteršičI edit answer – stasovlas

0

コードのこの作品を試してみてください。

.then(function(directories) { 
    return directories.map(function(directory) { 
    return fs.readdirAsync(path.join(rootPath, directory)) 
    ... 

は、コールバックに約束の配列を返します。約束事の配列はここでは即値としてカウントされ、配列の約束には結び付けられません。あなたがしなければならないのはPromise.mapを使用することです

:あなたはPromise.allを使用することができ、アレイの約束に約束の配列を変換していますが、青い鳥を使用しているので、あなたはより良いオプションを持っている

.then(function(directories) { 
    return Promise.map(directories, function(directory) { 
    return fs.readdirAsync(path.join(rootPath, directory)) 
    ... 
0

は、あまりにも多くの1 .thenは、第二の機能の上にあった、それを考え出した:

const Promise = require('bluebird'); 
const fs = Promise.promisifyAll(require('fs')); 
const path = require('path'); 

function getFileNames(rootPath) { 
    // Read content of path 
    return fs.readdirAsync(rootPath) 
     .then(function(content) { 
     return content.filter(function(file) { 
      return fs.statSync(path.join(rootPath, file)).isDirectory(); 
     }); 
     }) 
     // For every directory 
     .map(function(directory) { 
     // Read files in the directory 
     return fs.readdirAsync(path.join(rootPath, directory)) 
      .filter(function(file) { 
       return path.extname(file) == '.XML'; 
      }); 
     }); 
} 

getFileNames('./XML').then(function(files) { 
    console.log(files); 
}); 
関連する問題