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

Async methods in memory-fs don't work with promisify in native node module util #50

Open
Jocs opened this issue Oct 24, 2017 · 2 comments

Comments

@Jocs
Copy link

Jocs commented Oct 24, 2017

const { promisify } = require('util')
const fs = require('fs')
promisify(fs.readFile)('/hello.txt', 'utf-8')  // work well
.then(console.log.bind(console))
.catch(console.log.bind(console))

// but...when I use `memory-fs` in my project...

const MemoryFileSystem = require('memory-fs')
const fs = new MemoryFileSystem()
promisify(fs.readFile)('/hello.txt', 'utf-8') // dose not work
.then(console.log.bind(console))
.catch(console.log.bind(console))

 //  I need write this way `promisify(fs.readFile.bind(fs))` to make it work well,
// I think `memory-fs` should do this for users. is this an issue ?
@nrkn
Copy link

nrkn commented Nov 14, 2017

It's because of how MemoryFileSystem is a class. It doesn't really need to be - I might fork this if they don't intend to fix this, because it's kind of annoying. Native fs doesn't need bind, this package should work the same so that it's a drop in replacement and you don't have to change your client code.

@GeorgeTaveras1231
Copy link

In the past, I worked around this by creating what I've been calling a "bound proxy".

const fs = new Proxy(
  new MemoryFileSystem(),
  {
    get(obj, prop) {
      if (typeof obj[prop] === 'function') {
        return obj[prop].bind(obj);
      }

      return obj[prop]
    }
  }
);

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

3 participants