简体   繁体   中英

Hapijs , fs.readfile, fs.writefile, and childprocess.exec how do i control my async?

Why is it that when I execute this code list is empty am i doing my asynchronous calls incorrectly? I have been moving things around and separating them into functions but still have a race going on between my execution.

The order I would like the to execute is in their nesting but I guess something is wrong because it is 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 use async ( https://github.com/caolan/async ), it has several ways to handle the async calls, waterfall seens good for your problem:

async.waterfall([
  function(callback){
    callback(null, 'one', 'two');
  },
  function(arg1, arg2, callback){
    // arg1 now equals 'one' and arg2 now equals 'two'
      callback(null, 'three');
  },
  function(arg1, callback){
      // arg1 now equals 'three'
      callback(null, 'done');
  }
], function (err, result) {
  // result now equals 'done'    
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM