lib/Queryable.js
function()
    {
        if (this._executePromise)
            return this._executePromise;

        var q = this.clone();

        var table  = q._T.__name__;
        var page   = q._page;
        var fields = q._fields;

        // Create the query from our stuff
        var query = {};
        _(q._where.concat(q._like)).each(function (q) {
            query[q.key] = q.value;
        });

        var doneLoading      = Q.defer();
        this._executePromise = doneLoading.promise;

        var left = q._take;

        // A single iteration of fetching via the API. Called repeatedly in
        // the case of large / unbounded TAKES
        var loop = function() {

            // always take 1000 unless we're almost done
            var take = left < 1000 ? left : 1000;

            // Hit the API and incremement the page
            q._fetch(table, take, page++, query, fields).then(function(res) {

                // publish progress
                doneLoading.notify(q._results.length);

                if (left !== undefined) left -= res.length;

                // Either resolve the the promise with the results if we're
                // done or call the loop again

                if (q._doneLoading || left <= 0)
                    doneLoading.resolve(q._getResults());
                else
                    process.nextTick(loop);

            }).catch(function(err) {
                doneLoading.reject(err);
            });
        };

        // do tha damn thing
        loop();
        return doneLoading.promise;
    }
function()
    {
        if (this._executePromise)
            return this._executePromise;

        var q = this.clone();

        var table  = q._T.__name__;
        var page   = q._page;
        var fields = q._fields;

        // Create the query from our stuff
        var query = {};
        _(q._where.concat(q._like)).each(function (q) {
            query[q.key] = q.value;
        });

        var doneLoading      = Q.defer();
        this._executePromise = doneLoading.promise;

        var left = q._take;

        // A single iteration of fetching via the API. Called repeatedly in
        // the case of large / unbounded TAKES
        var loop = function() {

            // always take 1000 unless we're almost done
            var take = left < 1000 ? left : 1000;

            // Hit the API and incremement the page
            q._fetch(table, take, page++, query, fields).then(function(res) {

                // publish progress
                doneLoading.notify(q._results.length);

                if (left !== undefined) left -= res.length;

                // Either resolve the the promise with the results if we're
                // done or call the loop again

                if (q._doneLoading || left <= 0)
                    doneLoading.resolve(q._getResults());
                else
                    process.nextTick(loop);

            }).catch(function(err) {
                doneLoading.reject(err);
            });
        };

        // do tha damn thing
        loop();
        return doneLoading.promise;
    }
function ( ) { if ( this . _executePromise ) return this . _executePromise ; var q = this . clone ( ) ; var table = q . _T . __name__ ; var page = q . _page ; var fields = q . _fields ; // Create the query from our stuff var query = { } ; _ ( q . _where . concat ( q . _like ) ) . each ( function ( q ) { query [ q . key ] = q . value ; } ) ; var doneLoading = Q . defer ( ) ; this . _executePromise = doneLoading . promise ; var left = q . _take ; // A single iteration of fetching via the API. Called repeatedly in // the case of large / unbounded TAKES var loop = function ( ) { // always take 1000 unless we're almost done var take = left < 1000 ? left : 1000 ; // Hit the API and incremement the page q . _fetch ( table , take , page ++ , query , fields ) . then ( function ( res ) { // publish progress doneLoading . notify ( q . _results . length ) ; if ( left !== undefined ) left -= res . length ; // Either resolve the the promise with the results if we're // done or call the loop again if ( q . _doneLoading || left <= 0 ) doneLoading . resolve ( q . _getResults ( ) ) ; else process . nextTick ( loop ) ; } ) . catch ( function ( err ) { doneLoading . reject ( err ) ; } ) ; } ; // do tha damn thing loop ( ) ; return doneLoading . promise ; }
Actually hit the API. Returns a promise for the eventual value of these results. Ends up mutating the query and marking it as done  If we already have a promise, return that, meaning that it can only be eecuted once
Actually hit the API . Returns a promise for the eventual value of these results . Ends up mutating the query and marking it as done If we already have a promise return that meaning that it can only be eecuted once