2016-05-19 41 views
0

這是一個在我的流星應用程序中通過graphicsmagick來操縱一些圖像的功能。流星/ JS:在做另一個回調之前如何做回調

如果正在裁剪圖像,我想檢查它的寬度是否> 900px。然後它應該調整到900px。

該完整功能不起作用,因爲在執行return gmread.stream之前必須完成gmread.size()的回調 - 目前情況並非如此。但我不知道如何得到這個SYNCHRON/asynchron東西的工作...

function imageManipulation(inputId, method) { 
    var inStream = Files.findOneStream({ _id: inputId }), 
     gmread; 

    // do some image manipulation depending on given `method` 
    if (method == 'crop') { 
     gmread = gm(inStream); 

     // check if image width > 900px, then do resize 
     gmread.size(function(err, size) { 
      if (size.width > 900) { 
       gmread = gmread.resize('900'); 
      } 
     }); 
    } 

    return gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) { 
     stderr.pipe(process.stderr); 
     if (!err) { 
      var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) { 
       if (err) { console.warn("" + err); } 
       return; 
      }); 
      return stdout.pipe(outStream); 
     } 
    })); 

}); 

更新

我試圖用meteorhacks:async,但我得到的錯誤Exception in callback of async function: TypeError: Object [object Object] has no method 'size'

function imageManipulation(inputId, method) { 
    var inStream = Files.findOneStream({ _id: inputId }), 
     gmread; 

    if (method == 'crop') { 
     gmread = gm(inStream); 

     // Asnyc doesn't work 
     gmread = Async.runSync(function(done) { 
      gmread.size(function(err, size) { 
       if (size.width > 900) { 
        gmread = gmread.resize('900'); 
       } 
       done(null, gmread); 
      }); 
     }); 
    } 

    return gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) { 
     stderr.pipe(process.stderr); 
     if (!err) { 
      var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) { 
       if (err) { console.warn("" + err); } 
       return; 
      }); 
      return stdout.pipe(outStream); 
     } 
    })); 

}); 

回答

0

您可以使用meteorhacks:async暫停執行,直到您調用done()回調,如下所示。

function imageManipulation(inputId, method) { 
    var inStream = Files.findOneStream({ _id: inputId }), 
     gmread; 

    // do some image manipulation depending on given `method` 
    if (method == 'crop') { 
     gmread = gm(inStream); 

     // check if image width > 900px, then do resize 
     gmread.size(function(err, size) { 
      if (size.width > 900) { 
       gmread = gmread.resize('900'); 
      } 
     }); 
    } 

    var response = Async.runSync(function(done) { 

     gmread.stream('jpg', Meteor.bindEnvironment(function(err, stdout, stderr) { 
     stderr.pipe(process.stderr); 
     if (!err) { 
      var outStream = Files.upsertStream({ _id: outputFileId }, {}, function(err, file) { 
       if (err) { console.warn("" + err); } 
       return; 
      }); 
      done(null, stdout.pipe(outStream)); 
      } 
     })); 
    }); 
    return response.result 
    }); 
+0

也許我對它的理解不正確,但是我正在等待'gored.size'的結果進一步到流函數。你反過來......?! – user3142695

+0

它有用嗎? –

+0

不,它沒有工作 – user3142695