Skip to content

Instantly share code, notes, and snippets.

@al6x
Created October 13, 2012 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al6x/3883920 to your computer and use it in GitHub Desktop.
Save al6x/3883920 to your computer and use it in GitHub Desktop.
Copy streams with callbacks
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