0

Right now I have a module with two methods. How can I access downloadFile function to be reused in the downloadFiles function. Right now it will throw exception that the downloadFile is not defined. Thanks in advance.

exports.downloadLib = {
    downloadFile: async function (fileUrl, dest) {
        const shell = require('node-powershell');

        let ps = new shell({
            executionPolicy: 'Bypass',
            noProfile: true
        });

        let commandString = `iwr ${fileUrl} -OutFile ${dest}`;
        ps.addCommand(commandString);

        try {
            await ps.invoke();
        } catch (e) {
            console.log(`ERROR - ${e}`);
        } finally {
            await ps.dispose();
            console.log(`finished download file ${dest}`)
        }
    },

    downloadFiles: function (fileUrls) {
        fileUrls.forEach(function (fileUrl) {
            downloadFile(fileUrl, fileUrl.substring(fileUrl.lastIndexOf('/') + 1))
        }, this);
    }
}       
2
  • 2
    this.downloadFile Commented Apr 17, 2017 at 15:36
  • You can use this.downloadFile inside downloadFiles. Commented Apr 17, 2017 at 15:37

2 Answers 2

1

Create a private method for the module. Separately, inject this into the module and also invoke it where you need to. The underscore prefix is a convention used by many to denote a private method.

async function _downloadFile(fileUrl, dest) {
  const shell = require('node-powershell');

  let ps = new shell({
    executionPolicy: 'Bypass',
    noProfile: true
  });

  let commandString = `iwr ${fileUrl} -OutFile ${dest}`;
  ps.addCommand(commandString);

  try {
    await ps.invoke();
  } catch (e) {
    console.log(`ERROR - ${e}`);
  } finally {
    await ps.dispose();
    console.log(`finished download file ${dest}`)
  }
}

exports.downloadLib = {
  downloadFile: _downloadFile,

  downloadFiles: function(fileUrls) {
    fileUrls.forEach(function(fileUrl) {
      _downloadFile(fileUrl, fileUrl.substring(fileUrl.lastIndexOf('/') + 1))
    }, this);
  }
}

You can also keep _downloadFile() inside the module and do something like this._downloadFile() or context._downloadFile(). You will need to define let context = this at the top of the module for the latter.

Sign up to request clarification or add additional context in comments.

2 Comments

easy to change whether or not you export _downloadFile
Let me admit your answer is simpler. I just gave the answer I knew worked, not arguing it's perfect. Hm also mine avoids this in case a developer has an aversion to it.
0
downloadFiles: function (fileUrls) {
    fileUrls.forEach(function (fileUrl) {
        this.downloadFile(fileUrl, fileUrl.substring(fileUrl.lastIndexOf('/') + 1))
    }, this);
}

downloadFile is not part of downloadFiles Scope, but of its context.

5 Comments

This ain't proper in this way. The context of this in that way is the global object.
@Jordan Kanchelov it is. Looks like you dont know about forEachs second param... Hint: this is not global...
If I use 'use strict' "this" is undefined in this case. In this case when I am not using it should point to the global ?
@JordanKanchelov NOPE!!! have a look at Function.prototype.call. Thats what forEach is doing. This is passed into the forEach loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.