0

プロミスの結果を元の呼び出し元に返して返すのに問題があります。

store.js

module.exports = {
    find: function *(storeRequest){
        if(!_gateway){
            _gateway = realGateway;
        }

        storeResponse.http.statusCode = 200;

        var stores = _gateway.find(storeRequest.id).next().value; // I want to be able to get the stores array back here ultimately by calling next() like I am trying to do here

        console.log(stores[0]); 
        //yield storeResponse;
    }
};

storeGateway.js

    module.exports = {
        find: function *(id){
            var stores = [];
            var entity;

            database.find.then(function(foundStores){

                    entity = testUtil.createStore(foundStores[0].id, foundStores[0].name);
                    console.log("ENTITY:");
                    console.log(entity);

                    stores.push(entity);

                    console.log("STORES[0]:");
                    console.log(stores[0]);

// I am getting the results here successfully so far when I console.log(stores[0])!  But now I want to return stores now from here and yield the array so it propogates up to the caller of storeGateway's find()
                   // yield entity; --- this doesn't work because I think I'm in the promise then scope
                }
            );

            //yield entity;  -- and this definitely won't work because it's not in the promise callback (then)
        }
    };

データベース.js

var co = require('co');
var pg = require('co-pg')(require('pg'));
var config = require('./postgreSQL-config');

var database = module.exports = {};

var _id;
var _foundStores;

database.find = co(function* poolExample(id) {

        var query = "Select id, name from stores";

        try {
            var connectionResults = yield pg.connectPromise(config.postgres);
            var client = connectionResults[0];
            var done = connectionResults[1];

            var result = yield client.queryPromise(query);
            done();

            console.log("PRINTING ROWS:");
            console.log(result.rows[0]);

            _foundStores = yield result.rows;

        } catch(ex) {
            console.error(ex.toString());
        }

        console.log("RESULTS!:");
        console.log(_foundStores);

        return _foundStores;
    });

上記のすべての console.log にデータが出力されています。storeGateway の find() メソッドからストアを返す方法がわかりません。プロミスの結果 (.then()) でストア配列を受け取っているためです。

(コード内の私のコメントを参照してください。見つかったストアをプロミスで返し、store.js の検索ジェネレーター関数から上流に戻そうとしています)。

4

2 に答える 2

1

ジェネレーターとを使用するポイントは、コルーチン ランナーに約束し、その結果を取得coできるyieldため、 を使用する必要がないことですthen

findでメソッドを作成することから始めますdatabase.js

database.find = co.wrap(function* poolExample(id) {
//                ^^^^^
    …
});

次に、storeGateway.jsあなたがしている必要があります

module.exports = {
    find: function*(id) {
        var foundStores = yield database.find(id);
        var entity = testUtil.createStore(foundStores[0].id, foundStores[0].name);
        console.log("ENTITY:", entity);
        var stores = [entity];
        console.log("STORES[0]:", stores[0]);
        return stores;
    }
};

(ジェネレーター関数を でラップするかもしれませんco.wrap(…))。

次に、store.jsあなたができること

module.exports = {
    find: co.wrap(function*(storeRequest) {
        if (!_gateway) _gateway = realGateway;
        storeResponse.http.statusCode = 200;
        var stores = yield* _gateway.find(storeRequest.id);
        // or        yield _gateway.find(storeRequest.id); if you did wrap it and it
        //                                                 returns a promise, not a generator
        console.log(stores[0]);
        return stores;
    })
};
于 2015-09-10T12:31:29.927 に答える