AutobahnJS:远程调用asynchronous函数

我试图通过WAMP拨打一个远程function。 但是如果它有asynchronous行为,我不知道如何编写被调用的函数。 在我看到的每个例子中,远程函数都返回结果。 如何以asynchronous的方式来完成,我通常会使用callback?

例子:这是一个函数的注册,它将asynchronous获取文件的内容。

session.register('com.example.getFileContents', getFileContents).then( function (reg) { console.log("procedure getFileContents() registered"); }, function (err) { console.log("failed to register procedure: " + err); } ); 

以下是我将如何远程调用该function。

 session.call('com.example.getFileContents', ["someFile.txt"]).then( function (res) { console.log("File Contents:", res); }, function (err) { console.log("Error getting file contents:", err); } ); 

但是这是注册的实际function。

 function getFileContents(file) { fs.readFile(file, 'utf8', function(err, data) { // How do I return the data? }); } 

如何从getFileContents返回数据,以便通过WAMP连接发回数据? 我知道我可以使用readFileSync并返回它返回的内容。 但我特别要求如何以asynchronous的方式做到这一点。

我想通过承诺如何做到这一点。 这是函数如何用promise实现的。

 var fs = require('fs'); var when = require('when'); function getFileContents(file) { var d = when.defer(); fs.readFile(file, 'utf8', function(err, data) { d.resolve(data); }); return d.promise; }