javascript - Callback on recursive function -


the following code used fetch .zip file our web application. file generated closing application safely zipping, sending download.

var dl = function() {     request({         method: 'get',         uri: 'some_url',         headers: {             'user-agent': 'scripted-download'         },         encoding: null,         jar: true     }, function(err, res, body) {         if (err) throw(err)         if (res.headers['content-type'] === 'application/zip;charset=utf-8') {             process.stdout.write('\rdownloading file ..')             var id = uuid.v4()             , file = path.resolve(__dirname, '../../' + id + '.zip')             fs.writefile(file, body, function(err) {                 if (err) throw(err)                 process.stdout.write('\rfile downloaded ' + id + '.zip')                 process.exit(0)             })         } else {             process.stdout.write('\rawaiting file ..')             settimeout(dl(), 30 * 1000)         }     }) } 

this works expected. however, need use script. above code returns id of file downloaded, script can extract .zip , place extracted files directory same id. these files made available download.

edit need execute script, extract contents when it's downloaded load ui res.render() when previous 2 steps complete. needs done id 2 users don't create conflicting files.

as mentioned in comments, promises should make easy. first promisify async functionality need:

function makerequest(parameters) {     return new promise(function (resolve, reject) {         request(parameters,  function (err, res, body) {             if (err) { reject (err); }             else { resolve({ res: res, body: body }); }         });     }); }  function writefile(file, body) {     return new promise(function (resolve, reject) {         fs.writefile(file, body, function(err) {             if (err) { reject(err); }             else { resolve(); }         });     }); }  function timeout(duration) {     return new promise(function (resolve) {         settimeout(resolve, duration);     }); } 

then use them.

var dl = function () {     return makerequest({         method: 'get',         uri: 'some_url',         headers: {             'user-agent': 'scripted-download'         },         encoding: null,         jar: true     }).then(function (result) {         if (result.res.headers['content-type'] === 'application/zip;charset=utf-8') {             process.stdout.write('\rdownloading file ..')             var id = uuid.v4()             , file = path.resolve(__dirname, '../../' + id + '.zip');              return writefile(file, result.body)                 .then(function () { return id; });         } else {             process.stdout.write('\rawaiting file ..');              return timeout(30 * 1000).then(dl);         }     }); }  dl().then(function (id) { process.stdout.write('\rid is: ' + id); }); 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -