lib/sync.js
function SyncFuture(timeout)
{
    var self = this;
    
    this.resolved = false;
    this.fiber = Fiber.current;
    this.yielding = false;
    this.timeout = timeout;
    this.time = null;
    
    this._timeoutId = null;
    this._result = undefined;
    this._error = null;
    this._start = +new Date;
    
    Sync.stat.totalFutures++;
    Sync.stat.activeFutures++;
    
    // Create timeout error to capture stack trace correctly
    self.timeoutError = new Error();
    Error.captureStackTrace(self.timeoutError, arguments.callee);
    
    this.ticket = function Future()
    {
        // clear timeout if present
        if (self._timeoutId) clearTimeout(self._timeoutId);
        // measure time
        self.time = new Date - self._start;
        
        // forbid to call twice
        if (self.resolved) return;
        self.resolved = true;

        // err returned as first argument
        var err = arguments[0];
        if (err) {
            self._error = err;
        }
        else {
            self._result = arguments[1];
        }
        
        // remove self from current fiber
        self.fiber.removeFuture(self.ticket);
        Sync.stat.activeFutures--;
        
        if (self.yielding && Fiber.current !== self.fiber) {
            self.yielding = false;
            self.fiber.run();
        }
        else if (self._error) {
            throw self._error;
        }
    }
    
    this.ticket.__proto__ = this;
    
    this.ticket.yield = function() {
        while (!self.resolved) {
            self.yielding = true;
            if (self.timeout) {
                self._timeoutId = setTimeout(function(){
                    self.timeoutError.message = 'Future function timed out at ' + self.timeout + ' ms';
                    self.ticket(self.timeoutError);
                }, self.timeout)
            }
            Fiber.yield();
        }
        if (self._error) throw self._error;
        return self._result;
    }
    
    this.ticket.__defineGetter__('result', function(){
        return this.yield();
    });
    
    this.ticket.__defineGetter__('error', function(){
        if (self._error) {
            return self._error;
        }
        try {
            this.result;
        }
        catch (e) {
            return e;
        }
        return null;
    });
    
    this.ticket.__defineGetter__('timeout', function(){
        return self.timeout;
    });
    
    this.ticket.__defineSetter__('timeout', function(value){
        self.timeout = value;
    });
    
    // append self to current fiber
    this.fiber.addFuture(this.ticket);
    
    return this.ticket;
}
function SyncFuture(timeout)
{
    var self = this;
    
    this.resolved = false;
    this.fiber = Fiber.current;
    this.yielding = false;
    this.timeout = timeout;
    this.time = null;
    
    this._timeoutId = null;
    this._result = undefined;
    this._error = null;
    this._start = +new Date;
    
    Sync.stat.totalFutures++;
    Sync.stat.activeFutures++;
    
    // Create timeout error to capture stack trace correctly
    self.timeoutError = new Error();
    Error.captureStackTrace(self.timeoutError, arguments.callee);
    
    this.ticket = function Future()
    {
        // clear timeout if present
        if (self._timeoutId) clearTimeout(self._timeoutId);
        // measure time
        self.time = new Date - self._start;
        
        // forbid to call twice
        if (self.resolved) return;
        self.resolved = true;

        // err returned as first argument
        var err = arguments[0];
        if (err) {
            self._error = err;
        }
        else {
            self._result = arguments[1];
        }
        
        // remove self from current fiber
        self.fiber.removeFuture(self.ticket);
        Sync.stat.activeFutures--;
        
        if (self.yielding && Fiber.current !== self.fiber) {
            self.yielding = false;
            self.fiber.run();
        }
        else if (self._error) {
            throw self._error;
        }
    }
    
    this.ticket.__proto__ = this;
    
    this.ticket.yield = function() {
        while (!self.resolved) {
            self.yielding = true;
            if (self.timeout) {
                self._timeoutId = setTimeout(function(){
                    self.timeoutError.message = 'Future function timed out at ' + self.timeout + ' ms';
                    self.ticket(self.timeoutError);
                }, self.timeout)
            }
            Fiber.yield();
        }
        if (self._error) throw self._error;
        return self._result;
    }
    
    this.ticket.__defineGetter__('result', function(){
        return this.yield();
    });
    
    this.ticket.__defineGetter__('error', function(){
        if (self._error) {
            return self._error;
        }
        try {
            this.result;
        }
        catch (e) {
            return e;
        }
        return null;
    });
    
    this.ticket.__defineGetter__('timeout', function(){
        return self.timeout;
    });
    
    this.ticket.__defineSetter__('timeout', function(value){
        self.timeout = value;
    });
    
    // append self to current fiber
    this.fiber.addFuture(this.ticket);
    
    return this.ticket;
}
function SyncFuture ( timeout ) { var self = this ; this . resolved = false ; this . fiber = Fiber . current ; this . yielding = false ; this . timeout = timeout ; this . time = null ; this . _timeoutId = null ; this . _result = undefined ; this . _error = null ; this . _start = + new Date ; Sync . stat . totalFutures ++ ; Sync . stat . activeFutures ++ ; // Create timeout error to capture stack trace correctly self . timeoutError = new Error ( ) ; Error . captureStackTrace ( self . timeoutError , arguments . callee ) ; this . ticket = function Future ( ) { // clear timeout if present if ( self . _timeoutId ) clearTimeout ( self . _timeoutId ) ; // measure time self . time = new Date - self . _start ; // forbid to call twice if ( self . resolved ) return ; self . resolved = true ; // err returned as first argument var err = arguments [ 0 ] ; if ( err ) { self . _error = err ; } else { self . _result = arguments [ 1 ] ; } // remove self from current fiber self . fiber . removeFuture ( self . ticket ) ; Sync . stat . activeFutures -- ; if ( self . yielding && Fiber . current !== self . fiber ) { self . yielding = false ; self . fiber . run ( ) ; } else if ( self . _error ) { throw self . _error ; } } this . ticket . __proto__ = this ; this . ticket . yield = function ( ) { while ( ! self . resolved ) { self . yielding = true ; if ( self . timeout ) { self . _timeoutId = setTimeout ( function ( ) { self . timeoutError . message = 'Future function timed out at ' + self . timeout + ' ms' ; self . ticket ( self . timeoutError ) ; } , self . timeout ) } Fiber . yield ( ) ; } if ( self . _error ) throw self . _error ; return self . _result ; } this . ticket . __defineGetter__ ( 'result' , function ( ) { return this . yield ( ) ; } ) ; this . ticket . __defineGetter__ ( 'error' , function ( ) { if ( self . _error ) { return self . _error ; } try { this . result ; } catch ( e ) { return e ; } return null ; } ) ; this . ticket . __defineGetter__ ( 'timeout' , function ( ) { return self . timeout ; } ) ; this . ticket . __defineSetter__ ( 'timeout' , function ( value ) { self . timeout = value ; } ) ; // append self to current fiber this . fiber . addFuture ( this . ticket ) ; return this . ticket ; }
Future object itself
Future object itself