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

Return Objects from fs.read & fs.write #436

Closed
teledemic opened this issue May 31, 2017 · 3 comments
Closed

Return Objects from fs.read & fs.write #436

teledemic opened this issue May 31, 2017 · 3 comments
Labels

Comments

@teledemic
Copy link

teledemic commented May 31, 2017

The promisified version of fs.read shows up in the typescript typings, but when run gives "fs.read is not a function". I'm using the latest versions of both:

		"fs-extra": "3.0.1",
		"@types/fs-extra": "3.0.2",

and calling as I would expect to:

let [bytesread, buffer] = await fs.read(fd, buf, 0, length, start);
@RyanZim
Copy link
Collaborator

RyanZim commented Jun 1, 2017

This is due to the way our promisification logic works (or rather, doesn't work). The promise only returns bytesread, not the buffer. So usage would be:

let buf = Buffer.alloc(length)
let bytesread = await fs.read(fd, buf, 0, length, start)
// Use buf here

The reason for this is that fs.read calls the callback with 3 arguments, but our promisification logic only passes the second argument in the promise.

This is sub-optimal and should probably be fixed in the next major release. However, I'm thinking we should follow the lead of util.promisify's behavior. From the docs:

If this method [fs.read()] is invoked as its util.promisify()ed version, it returns a Promise for an object with bytesRead and buffer properties.

CC: @jprichardson @manidlou

@teledemic
Copy link
Author

Thanks! That works great, and having two references to the same buffer was redundant anyway. The typescript defs are still showing it returning [number, Buffer], but that's not an issue for this repo.

@RyanZim
Copy link
Collaborator

RyanZim commented Jun 1, 2017

Yeah, could submit a fix here: https://github.com/DefinitelyTyped/DefinitelyTyped

@RyanZim RyanZim changed the title fs.read is not a function Return Objects from fs.read & fs.write Jun 21, 2017
Borewit added a commit to Borewit/strtok3 that referenced this issue Jun 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants