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

11
  • You should explain what you are actually trying to do as well. Commented Aug 2, 2015 at 14:14
  • how about this: stackoverflow.com/questions/28521117/… Commented Aug 2, 2015 at 14:16
  • @thefourtheye- I'll update my question ASAP with the data you ask Commented Aug 2, 2015 at 14:17
  • @thefourtheye -Done ! Commented Aug 2, 2015 at 14:18
  • @shopiaT After parsing you are ignoring it. Commented Aug 2, 2015 at 14:19

1 Answer 1

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) { … })
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks I try it and I got error undefined is not a function in the .catch(function(err) { ,any idea?
@shopiaT: Please log the result of the globAsync call to see what it is. Promisification should work fine on glob.
How should I log it ?
But how I got error when I run it ,Do you want me to change the code?
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.