Javascript 从发电机功能中退出最终承诺范围

Javascript 从发电机功能中退出最终承诺范围,javascript,node.js,promise,generator,co,Javascript,Node.js,Promise,Generator,Co,我无法将承诺结果作为收益返回给原始调用方 store.js module.exports = { find: function *(storeRequest){ if(!_gateway){ _gateway = realGateway; } storeResponse.http.statusCode = 200; var stores = _gateway.find(storeRequest.i

我无法将承诺结果作为收益返回给原始调用方

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;
    }
};
    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)
        }
    };
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;
    });
storeGateway.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;
    }
};
    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)
        }
    };
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;
    });
database.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;
    }
};
    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)
        }
    };
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()方法返回存储,因为它在promise结果(在.then()中)中接收存储数组,我需要能够返回到上游


(请参阅我在代码中的注释,我正在尝试返回promise中找到的存储,然后从我的store.js的find generator函数返回上游)。

有两种方法可以做到这一点。您可以在函数中接收回调参数,并在承诺得到解决时调用它(在then函数中),或者更好地返回then的结果。然后()本身返回一个承诺,并且您从函数中返回的任何内容都可用于链接到该承诺的后续函数,因此如果您这样做

return 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]);
       return stores[0];
}

然后,您可以执行gateway.find()。然后(函数(stores){})和stores将是您返回的内容,即stores[0]。

使用生成器和
co
的意义在于,您可以
向协同程序运行者提供
承诺,并获得它们的结果,这样您就不必使用
然后

首先在
数据库.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;
    })
};

\u foundStores=yield result.rows结果行[0]
,则code>毫无意义。你为什么这么做?日志中的
行[0]
是什么(或者:您希望它是什么)?是的,之前的只是测试坦克是的,我还是发电机的新手,将尝试一下,谢谢!获取对象不是var foundStores=yield database.find()上的函数;是的,它不起作用了。在我用co-tooAh包装store.js之后,我一直在获取对象不再是函数。我把
co
误认为
co.wrap
。我没有意识到您确实创建了
数据库。查找
是一个承诺,而不是一个承诺返回函数。还是你不打算这么做?不知道co wrap