Skip to content

Commit 46c8c94

Browse files
ronkorvingjasnell
ronkorving
authored andcommitted
fs: reduced duplicate code in fs.write()
PR-URL: #2947 Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent a04408a commit 46c8c94

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

lib/fs.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -614,25 +614,21 @@ fs.readSync = function(fd, buffer, offset, length, position) {
614614
// OR
615615
// fs.write(fd, string[, position[, encoding]], callback);
616616
fs.write = function(fd, buffer, offset, length, position, callback) {
617-
function strWrapper(err, written) {
617+
function wrapper(err, written) {
618618
// Retain a reference to buffer so that it can't be GC'ed too soon.
619619
callback(err, written || 0, buffer);
620620
}
621621

622-
function bufWrapper(err, written) {
623-
// retain reference to string in case it's external
624-
callback(err, written || 0, buffer);
625-
}
626-
627622
var req = new FSReqWrap();
623+
req.oncomplete = wrapper;
624+
628625
if (buffer instanceof Buffer) {
629626
// if no position is passed then assume null
630627
if (typeof position === 'function') {
631628
callback = position;
632629
position = null;
633630
}
634631
callback = maybeCallback(callback);
635-
req.oncomplete = strWrapper;
636632
return binding.writeBuffer(fd, buffer, offset, length, position, req);
637633
}
638634

@@ -648,7 +644,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
648644
length = 'utf8';
649645
}
650646
callback = maybeCallback(position);
651-
req.oncomplete = bufWrapper;
652647
return binding.writeString(fd, buffer, offset, length, req);
653648
};
654649

0 commit comments

Comments
 (0)