Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

[REVISED]

var dir = require('node-dir');
var files = function getFiles(){

        return new Promise(function (resolve, reject) {
        dir.files('../sample/sample', function (err, res){
            if (err) {
                    reject(err);
                    console.log('Oops, an error occured %s', err);
            }
            else {
                      resolve(res);
                      return res;
                  }
            });
        });
}

console.log(files);

module.exports = files;

UPDATE AGAIN:

//read.js

var dir = require('node-dir');
var Promise = require('promise');


module.exports = new Promise(function (resolve, reject) {
        dir.files('../sample/sample', function (error, results){
            if (error) {
                    reject(error);
                    console.log('Oops, an error occured %s', err);
            }
            else {
                      resolve(results);
                  }
            });
        })

//app.js

var filelist = require('./read');

filelist.then(function (res){

    console.log(res);
})

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?

like image 582
Joe Avatar asked May 14 '17 08:05

Joe


People also ask

Can I have two module exports?

Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.

Can you export with module exports?

Exporting values with just the exports keyword is a quick way to export values from a module. You can use this keyword at the top or bottom, and all it does is populate the module. exports object. But if you're using exports in a file, stick to using it throughout that file.

How does module export work?

module. Exports is the object that is returned to the require() call. By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

Why would you create a default export in a module?

Default Exports: Default exports are useful to export only a single object, function, variable. During the import, we can use any name to import.


1 Answers

This is my code

read.js

var dir = require('node-dir');

var files = 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);
        }
    });
});

exports.files = files;

And then use it as such on the file that imports this module.

app.js

var read = require('read.js');

read.files.then(function(res) {
    console.log(res);
});

Your revised code

read.js

var dir = require('node-dir');

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

exports.files = files;

app.js

var read = require('read.js');

read.files().then(function(res) {
    console.log(res);
});

Hope that helps.

like image 193
Achshar Avatar answered Sep 23 '22 15:09

Achshar