Sunday, 14 May 2017

Export module after promise completes

I actually want to put the "read" function in a different module and then require it in my main app.js. (I'm quite new to using promises)

var dir = require('node-dir');
var promise = new Promise(function (resolve, reject) {
              dir.files('../sample/sample.txt', function (err, res){
                      if (err) 
                        {
                            reject(err);
                            console.log('Oops, an error occured %s', err);
                        }
                      else {
                            resolve(res);
                       }
                    });
                }).then(function (res){

                        console.log(res);
                        //only export here once I get the list
                });

How do I go about that? I've tried "return new promise..." and putting the whole thing in a getFile function, but it doesn't return anything.

What am I doing wrong?



via Joe

No comments:

Post a Comment