Closed bugs181 closed 10 years ago
Update:
Went ahead and added a setContext
method for those pesky modules that needed it. ;)
Function.prototype.setContext = function(obj) {
this.__context = obj;
}
Inside Function.prototype.sync = function(obj /* arguments */)
// call async function
if (this.__context) {
this.apply(this.__context, args);
} else {
this.apply(null, args);
}
Example:
var Sync = require('sync');
Sync(function() {
do_something_useful.setContext( this );
var result = do_something_useful.sync("param 1", "some other param");
});
I was having a problem adapting old projects where I wanted a custom
Function.prototype
that could switch between normal callback style and.sync()
style.For that reason I made a small minor modification that allows you to do something like:
Rather than
do_something_useful.sync(null, "param 1", "some other param");
The modification is to remove the arguments loop, and ignore the context object inside the
.sync()
function.I'm only sharing this because I thought it might help somebody else. Cheers.
NOTE: You WILL lose the ability to have a context. I.E. You can no longer use
this
or a reference to any other object for.sync()
. Please understand this before making the modification.