Aks*_*hat 5 asynchronous sync meteor node-fibers
这一直困扰着我一段时间,所以我想我只是快速做一个QA:
如果有一个普通的nodeJS模块或其他东西,它在服务器端有一个异步功能.如何使其同步.例如,我如何将nodejs fs.stat异步函数转换为同步函数.
我有
服务器端js
Meteor.methods({
getStat:function() {
fs.stat('/tmp/hello', function (err, result) {
if (err) throw err;
console.log(result)
});
}
});
Run Code Online (Sandbox Code Playgroud)
如果我从客户端调用它,我会得到undefined结果,因为结果是回调.
有一个函数(未记录)被调用Meteor.wrapAsync.
只需将功能包起来
Meteor.methods({
getStat:function() {
var getStat = Meteor._wrapAsync(fs.stat);
return getStat('/tmp/hello');
}
});
Run Code Online (Sandbox Code Playgroud)
现在,你会得到这样的结果在result你的Meteor.call.您可以转换任何具有回调的异步函数,其中第一个参数是错误,第二个参数是结果.
| 归档时间: |
|
| 查看次数: |
446 次 |
| 最近记录: |