使用Bluebird函数

时间:2016-07-03 22:17:12

标签: node.js csv asynchronous promise bluebird

我是初学者,在节点js中使用promise bluebird来读取所有csv文件并在bluebird函数中转换为json,但由于某种原因我无法正确使用它。

var Promise = require('bluebird');
var join = Promise.join;
var fs = Promise.promisifyAll(require("fs"));

var Converter = Promise.promisifyAll(require("csvtojson")).Converter;


Promise.promisifyAll(Converter.prototype);
var conversionJsons = Promise.promisifyAll(jsons);

function jsons (data){
  return {
    then: function(callback){
      var converter = new Converter({});
      converter.fromStringAsync(data).then(function(result){
        console.log("results are: "+ JSON.stringify(result));  
        callback(result);
      });
    }
  };
};


fs.readdirAsync(dir).map(function(fileName) {

  var contents = fs.readFileAsync(fileName).catch(function ignore() {});
    return join(fileName, contents, function(fileName, contents) {
        return {
          fileName: fileName,
          contents: contents
        }
    });


}).each(function(file) {

    conversionJsons(file.contents.toString()).then(function(result){
      console.log("Size: "+ result.length);
    });

}).then(function(contents) {
    console.log('Done.............');
})

我的步骤是:

  1. 使用fs模块读取所有csv文件。
  2. 将csv的内容转换为json。
  3. 在读取所有文件时报告'完成....'。
  4. 这是我的输出。

    完成.............

    结果为:[{“Order_id”:1,“数量”:70,“价格”:100},{“Order_id”:2,“数量”: 50, “价格”:100},{ “ORDER_ID”:3, “数量”:101, “价格”:10}]

    结果为:3

    console.log('Done .............')在步骤1和2之前调用。

    编辑:

    这是我的最新更新。我使函数jsons成为一个promise函数。

    var Promise = require('bluebird');
    var join = Promise.join;
    var fs = Promise.promisifyAll(require("fs"));
    
    var Converter = Promise.promisifyAll(require("csvtojson")).Converter;
    
    
    Promise.promisifyAll(Converter.prototype);
    var conversionJsons = Promise.promisifyAll(jsons);
    
    function jsons (data){
      return {
        then: function(callback){
        var convertion = new Promise(function(resolve, reject){
        var converter = new Converter({});
        var output="";
        converter.fromStringAsync(data).then(function(result){
          console.log('processing');
          return result;
        });
    
          });
        }
      };
    };
    
    fs.readdirAsync(dir).map(function(fileName) {
    
      var contents = fs.readFileAsync(fileName).catch(function ignore() {});
        return join(fileName, contents, function(fileName, contents) {
            return {
              fileName: fileName,
              contents: contents
            }
        });
    
    }).each(function(file) {
      console.log("results are: "+ file.contents);
      return conversionJsons(file.contents.toString()).then(function(result){
        console.log("Size: "+ result.length);
      });
    }).then(function(result) {
      console.log("Final results are: "+ JSON.stringify(result)); 
      console.log('Done.............');
    })
    

    我的输出

    结果是:Order_id,数量,价格 1,70,100 2,50,100 3,101,10

    最终结果是:[{“fileName”:“pizza.csv”,“contents”:{“type”:“Buffer”,“data”:[ 79,114,100,101,114,95,105,100,44,81,117,97,110,116,105,116,121,44,80,114,105,99, 101,13,10,49,44,55,48,44,49,48,48,13,10,50,44,53,48,44,49,48,48,13,10,51,44, 49,4 8,49,44,49,48,13,10]}}]

    完成.............

    处理

0 个答案:

没有答案