javascript - Hapijs , fs.readfile, fs.writefile, and childprocess.exec how do i control my async? -
javascript - Hapijs , fs.readfile, fs.writefile, and childprocess.exec how do i control my async? -
why when execute code list empty doing asynchronous calls incorrectly? have been moving things around , separating them functions still have race going on between execution.
the order execute in nesting guess wrong because not working.
server.route({ method: 'post', path: '/convert', config: { validate: { payload: { fileupload: joi.object({ headers: joi.object({ 'content-type': joi.string().valid(['application/pdf']).required(), }).unknown().required() }).unknown() } }, payload: { output: 'file', maxbytes: 209715200, uploads:'./thumbs' }, handler: function(request, reply) { var newpath, filename, filenamepng; fs.readfile(request.payload.fileupload.path, function(err, data) { filename = request.payload.fileupload.filename; filenamepng = filename.substr(0, filename.indexof('.')); newpath = __dirname + "/thumbs/"; fs.writefile(newpath + filename, data, function(err) { childprocess.exec('cd ' + newpath + ' && convert ' + filename + ' -thumbnail 50% ' + filenamepng + '_thumb_page_' + '%d' + '.png', function(error) { fs.readdir(__dirname + "/thumbs", function(error, list) { list.foreach(function(element) { element = element.tostring(); thumbs.addthumbnail(thumbs.thumbnailarray, {name: element, url: './thumbs/' + element }); }); var tmpfn = request.payload.fileupload.path; fs.unlink(tmpfn, function(err){ if(err){ console.log(err); } else { console.log('/tmp/file deleted') // reply(thumbs.showthumbnails(thumbs.thumbs)); }; }); reply(thumbs.showthumbnails(thumbs.thumbs)); }); }); }); process.on('exit', function(code) { console.log('process finished'); }); }); } } });
you should totally utilize async (https://github.com/caolan/async), has several ways handle async calls, waterfall seens problem:
async.waterfall([ function(callback){ callback(null, 'one', 'two'); }, function(arg1, arg2, callback){ // arg1 equals 'one' , arg2 equals 'two' callback(null, 'three'); }, function(arg1, callback){ // arg1 equals 'three' callback(null, 'done'); } ], function (err, result) { // result equals 'done' }); javascript node.js asynchronous hapijs
Comments
Post a Comment