我正在使用這個美妙的同步模塊synchronize.js - http://alexeypetrushin.github.io/synchronize/docs/index.html。Node.js,Synchronize.js和返回值
我遇到了一種情況,我必須將sync'd函數的返回值放入光纖以外的範圍。下面是我在談論的一個基本的例子:
var records = sync.fiber(function() {
var results = ... // some synchronized function
return results;
});
而records
會,從理論上講,含有纖維範圍內的results
值。我一直在閱讀期貨(纖維/期貨模塊)以及它們如何在這種情況下使用,但我還沒有想出任何接近工作的東西。我喜歡一些方向和/或解決方案。
編輯:
對於我在尋找什麼來完成更徹底的例子:
// executes a stored procedure/function
exec: function (statement, parameters) {
init();
var request = new sql.Request(),
results;
processParams(parameters, request);
var res = sync.fiber(function(){
try {
var result = sync.await(request.execute(statement, sync.defers('recordsets', 'returnValue')));
results = result.recordsets.length > 0 ? result.recordsets[0] : [];
return results;
}
catch (e) {
console.log('error:connection:exec(): ' + e);
throw(e);
}
});
// though typical scope rules would mean that `results` has a
// value here, it's actually undefined.
// in theory, `res` would contain the return value from the `sync.fiber` callback
// which is our result set.
return res;
}
正如你可以看到這裏,我想做到的是得到的值從光纖的範圍來看,主要範圍爲results
。