简体   繁体   English

流星:如何从Meteor.method返回之前等待函数返回?

[英]Meteor: How do I wait on functions returning before returning from my Meteor.method?

I have a Meteor.method that creates an image and writes css. 我有一个Meteor.method可以创建图像并编写CSS。 I have a spinner that displays until the method returns. 我有一个微调框,显示直到方法返回。 The method returns almost immediately before functions that create images and css have finished doing their thing. 该方法几乎在创建图像和CSS的函数完成其工作之前返回。

How do I include a callback to wait until the image and css has been written before returning from the Meteor.method? 从Meteor.method返回之前,如何包含回调以等待图像和CSS被写入之前?

This is my method: 这是我的方法:

createImage: function(coords) {
   console.log('1: createImage')

   var source="myimage.png";
   var im = gm.subClass({imageMagick: true});
   im(source)
    .crop()
     .write('newimage.png', function(err){
       if (err) return console.dir(arguments)
        console.log('4: image has been written')
     }) // end of write function


 fs.appendFile(directory + 'css.import.less', css, function (err) {
   if (err) throw err;
   console.log('3: The "data to append" was appended to file!');
 });

  console.log('2: image and css written')
  return true;
},

I've numbered the console logs in the order that they are displayed in. What I want is the console.log messages to be displayed in the order that they are written in. 我已经按照显示顺序对控制台日志进行了编号。我想要的是console.log消息,以它们被写入的顺序显示。

So instead of this: 所以代替这个:

//start of method
console.log('1: createImage')
console.log('4: image has been written')
console.log('3: The "data to append" was appended to file!');
console.log('2: image and css written')
//end of method

I expect this: 我期望这样:

//start of method
console.log('1: createImage')
console.log('2: image has been written')
console.log('3: The "data to append" was appended to file!');
console.log('4: image and css written')
//end of method

Currently the method is returning before the functions that write the image and css have returned. 当前,该方法在写图像和CSS的函数返回之前返回。 The effect of this is that the spinner is displaying for a split second instead of waiting till the functions that create the image and css have returned which means the spinner should be displaying for longer. 这样的效果是微调器仅显示一秒钟,而不是等到创建图像和css的函数返回后,这意味着微调器应显示更长的时间。

there's a thing called Promises in JS. JS中有一个叫做Promises的东西。 http://www.html5rocks.com/en/tutorials/es6/promises/ http://www.html5rocks.com/zh-CN/tutorials/es6/promises/

I think it may help you figure out. 我认为这可以帮助您找出答案。 the basics are so: 基本原理如下:

somePromiseShim(function(next) {
  console.log("first"); next();
}).then(function(data, next) {
  console.log("second"); next();
}).then(function(data, next) {
  console.log("third"); next();
});

you may ask yourself why you should use it - it helps doing async stuff syncish. 您可能会问自己为什么要使用它-它有助于使异步内容同步。 so lets add some async functionality to the code above. 因此,让我们在上面的代码中添加一些异步功能。

somePromiseShim(function(next) {
  console.log("first"); 
  setTimeout(function() { next(); }, 1000);
}).then(function(data, next) {
  console.log("second will fire up after 1 second"); next();
}).then(function(data, next) {
  console.log("third");
});

there is a simple implementation of Promise i did for a friend over here: https://gist.github.com/Schniz/0f525060aa9bec0b8d69 you might learn how to use it using this demo. 我在这里为朋友做了一个Promise的简单实现: https : //gist.github.com/Schniz/0f525060aa9bec0b8d69您可能会在此演示中学习如何使用它。

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

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