client.add(torrentId, torrent => { const files = torrent.files let length = files.length // Stream each file to the disk files.forEach(file => { const source = file.createReadStream() const destination = fs.createWriteStream(file.name) source.on('end', () => { console.log('file:\t\t', file.name) // close after all files are saved length -= 1 if (!length) process.exit() }).pipe(destination) }) })