I'm sorry if I'm missing something - I'm still learning Node's logic (esp. asynchronous jobs).
I want to download a zip file, then save it and unzip it. It looks like this:
request(URL)
.pipe(fs.createWriteStream(/path/to/filename))
.on('close', function () {
// I want to unzip and I need access to filename
});
Now the thing is, if I set a filename
variable beforehand, since it's in a loop, by the time the file gets downloaded, filename
will have the value of the last loop iteration.
Of course, I see we can do it by defining a function that would do all this, like fecthAndUnzip(URL,path)
. But my question is rather design-related: is it the only way we can do it in Node, or is there a better one? For example, can I access the filename in the on('close')
event and if so, should I? Thanks!