Created
October 13, 2012 09:16
-
-
Save al6x/3883920 to your computer and use it in GitHub Desktop.
Copy streams with callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var copy = function(inputStream, outputStream, callback){ | |
var copyNextChunk = function(){ | |
inputStream.read(fuction(err, chunk){ | |
if(err) return callback(err) | |
// When chunk == null there's no data, copying is finished. | |
if(!chunk) return callback() | |
outputStream.write(chunk, function(err){ | |
// Callback called only when chunk of data | |
// delivered to the recipient and | |
// we can send another one. | |
if(err) return callback(err) | |
copyNextChunk() | |
}) | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment