我正在尝试解决我想要执行两个同步函数的问题。我既孤立地工作,又一个接一个地工作。
我调用名为“createImage”的Meteor.method,并在服务器上调用另一个名为“writeImage”的方法。一旦文件被写入,我希望它然后在“writeCss”函数调用中编写CSS。
问题是没有调用“writeCss”调用。
有人可以建议一种方法来排序吗?我试图将我的代码清理一下,防止我的微调器过早终止。
Meteor.call('createImage', params, function(err, result){
if (!err) {
// remove the overlay on success
LoadingOverlay.destroyLoadingOverlay(selector);
}
});
createImage: function(params) {
console.log('write image');
Meteor.call('writeImage', params);
console.log('written image');
/* execution stops here */
console.log('write css')
Meteor.call('writeCss', params);
console.log('written css')
console.log('image and css written')
return true;
},
writeImage: function(){
writeImageAsync = function(gm, source, params, publicRoot){
console.log('in writeImageAsync')
var im = gm.subClass({imageMagick: true});
im(source)
.crop(params.a, params.b, params.c, params.d)
.write(publicRoot + 'myimage.png', function(err){
if (err) return console.dir(arguments)
console.log('image has been written')
/*
This is as far as it gets. Doesn't return from here
*/
})
};
writeImageSync = Meteor._wrapAsync(writeImageAsync);
writeImageSync(gm, source, params, publicRoot);
}
答案 0 :(得分:1)
使用Meteor._wrapAsync
时,将使用附加的回调参数调用包装函数。你必须在完成后调用该回调。回调遵循node.js约定 - 它的第一个参数是错误,如果没有错误,则返回null;第二个参数是返回值,如果有的话。
// you probably didn't mean to make a global here
writeImageAsync = function(gm, source, params, publicRoot, callback){
console.log('in writeImageAsync')
var im = gm.subClass({imageMagick: true});
im(source)
.crop(params.a, params.b, params.c, params.d)
.write(publicRoot + 'myimage.png', function(err){
if (err) {
console.dir(arguments);
callback(new Error("error writing image"));
} else {
console.log('image has been written');
callback(null);
}
});
};
writeImageSync = Meteor._wrapAsync(writeImageAsync);
writeImageSync(gm, source, params, publicRoot);