Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES6 Promise version .vm #42

Closed
Kailang opened this issue Jul 28, 2017 · 2 comments
Closed

ES6 Promise version .vm #42

Kailang opened this issue Jul 28, 2017 · 2 comments

Comments

@Kailang
Copy link

Kailang commented Jul 28, 2017

Trying to add a ES6 Promise version of the .mv function.

Here's the draft:

        mvPromise: function(path) {
          return new Promise((resolve, reject) => {
            let fstream = fs.createWriteStream(path);

            streamifier.createReadStream(buf).pipe(fstream);

            fstream.on('error', function(error) {
              reject(error);
            });

            fstream.on('close', function() {
              resolve();
            });
          });
        }

This should be appended after the .vm definition in let newFile.

Any suggestions? If this looks good, I'll create a pull request with a test.

@richardgirges
Copy link
Owner

Hey this looks good, but I'm wondering if we can just update the existing mv method instead. If a callback is passed in, it executes a callback. Otherwise, it returns a promise. Make sense?

@Kailang
Copy link
Author

Kailang commented Aug 4, 2017

Yeah, of course, why not?

        mv: function(path, callback) {
          if (callback) {
            let fstream = fs.createWriteStream(path);

            streamifier.createReadStream(buf).pipe(fstream);

            fstream.on('error', error => callback(error));
            fstream.on('close', () => callback(null));
          } else {
            return new Promise((resolve, reject) => {
              let fstream = fs.createWriteStream(path);

              streamifier.createReadStream(buf).pipe(fstream);

              fstream.on('error', error => reject(error));
              fstream.on('close', () => resolve(null));
            });
          }
        }

And if you dislike lambda expression...

        mv: function(path, callback) {
          if (callback) {
            let fstream = fs.createWriteStream(path);

            streamifier.createReadStream(buf).pipe(fstream);

            fstream.on('error', function (error) {
              callback(error);
            });
            
            fstream.on('close', function () {
              callback(null);
            });
          } else {
            return new Promise((resolve, reject) => {
              let fstream = fs.createWriteStream(path);

              streamifier.createReadStream(buf).pipe(fstream);

              fstream.on('error', function (error) {
                reject(error);
              });

              fstream.on('close', function () {
                resolve(null);
              });
            });
          }
        }

Plus a way to reduce one level of indention:

        mv: function(path, callback) {
          if (!callback) {
            return new Promise((resolve, reject) => {
              let fstream = fs.createWriteStream(path);

              streamifier.createReadStream(buf).pipe(fstream);

              fstream.on('error', error => reject(error));
              fstream.on('close', () => resolve(null));
            });
          }
          
          let fstream = fs.createWriteStream(path);

          streamifier.createReadStream(buf).pipe(fstream);

          fstream.on('error', error => callback(error));
          fstream.on('close', () => callback(null));
        }

And the old fashion:

        mv: function(path, callback) {
          if (!callback) {
            return new Promise((resolve, reject) => {
              let fstream = fs.createWriteStream(path);

              streamifier.createReadStream(buf).pipe(fstream);

              fstream.on('error', function (error) {
                reject(error);
              });

              fstream.on('close', function () {
                resolve(null);
              });
            });
          }

          let fstream = fs.createWriteStream(path);

          streamifier.createReadStream(buf).pipe(fstream);

          fstream.on('error', function (error) {
            callback(error);
          });

          fstream.on('close', function () {
            callback(null);
          });
        }

This was referenced Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants