简体   繁体   English

Hapijs,fs.readfile,fs.writefile和childprocess.exec如何控制异步?

[英]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( https://github.com/caolan/async ),它有几种处理异步调用的方法,瀑布看起来很适合您的问题:

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'    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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