2

I use the following code and I need to convert it to promise and at the end return object which contain the file configuration, how should I do that ?

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

module.exports = {
    parse: function (configErr) {
    glob("folder/*.json", function (err, files) {
      if (err) {
        return configErr(new Error("Error to read json files: " + err));
      }
      files.forEach(function (file) {
        fs.readFileAsync(file, 'utf8', function (err, data) { // Read each file
          if (err) {
            return configErr(new Error("Error to read config" + err));
          }
          return JSON.parse(data);
        });
      })
    })

UPDATE -in the code I want to get json files from specific folder in my node project and parse the json content to object

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88

1 Answers1

3

Promisified functions return promises, you should use those instead of passing callbacks into the invocation. Btw, your forEach loop does not work asynchronously, you should use a dedicated promise function for that.

 var Promise = require('bluebird'),
     globAsync = Promise.promisify(require("glob")),
     fs = Promise.promisifyAll(require("fs"));

module.exports.parse = function() {
    return globAsync("folder/*.json").catch(function(err) {
        throw new Error("Error to read json files: " + err);
    }).map(function(file) {
        return fs.readFileAsync(file, 'utf8').then(JSON.parse, function(err) {
            throw new Error("Error to read config ("+file+")" + err);
        });
    });
};

Then you can import this promise, and catch errors or use the array of parsed config objects by attaching callbacks to it via .then.

var config = require('config');
config.parse().then(function(cfg) { … }, function onConfigErr(err) { … })
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks I try it and I got error undefined is not a function in the .catch(function(err) { ,any idea? – 07_05_GuyT Aug 02 '15 at 15:02
  • @shopiaT: Please log the result of the `globAsync` call to see what it is. [Promisification](http://bluebirdjs.com/docs/api/promise.promisify.html) should work fine on `glob`. – Bergi Aug 02 '15 at 15:06
  • How should I log it ? – 07_05_GuyT Aug 02 '15 at 15:09
  • But how I got error when I run it ,Do you want me to change the code? – 07_05_GuyT Aug 02 '15 at 15:10
  • Yes, please use standard debugging techniques. If you don't want to point a debugger at node, edit the code and insert `console.log` statements where appropriate. – Bergi Aug 02 '15 at 15:11
  • I think I know what is the problem, assume that I want to call to this functionality parse(as my question post): function () { .... how should I call it ? can you please update the answer with it ? – 07_05_GuyT Aug 02 '15 at 15:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84936/discussion-between-shopia-t-and-bergi). – 07_05_GuyT Aug 02 '15 at 15:17
  • I've removed the `parse` method from the module as it looked quite unnecessary. The files start to load when the module is loaded, and the results are exported as a promise. You can use that like `require('config').then(function(cfg) { … }, function(err) { … })` – Bergi Aug 02 '15 at 15:18