Creating a sprite in node js from a buffer response

Im getting thumbnails back as a buffer that I want to return to a new image file. For the next response I would like to append the data to create a sprite building on to that first image creation. Something like this:

stream = fs.createWriteStream("test.jpg", { flags: "a" });

for pic in folder
  stream.write(buffer, function(){
    console.log "written data to file"       
  }

It creates one of the thumbnails but doesn't append the next. Don't really know what I'm doing wrong. Something with the positioning of the write? Sorry for the mix between coffeescript and js but the for loop is really not the focus of discussion here.