从生成器函数中退出 Final Promise 作用域

Exiting Final Promise Scope from Within a Generator Function

我在尝试 return 承诺结果作为回馈给原始调用者时遇到问题。

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)
        }
    };

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

(请参阅我在代码中的评论,我正在尝试 return 在 promise 中找到的存储然后从我的 store.js 的查找生成器函数返回上游)。

有两种方法可以做到这一点。你要么在你的函数中接收到一个回调参数,并在承诺被解决时调用它(在你的 then 函数中),要么更好,return then 的结果。 then() 本身 return 是一个承诺,无论您 return 在函数中使用 with 都可用于链接到承诺的后续函数,所以如果您这样做

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().then(function(stores){}) 并且 stores 将是您 return 编辑的内容,即 stores[0].

使用生成器和 co 的要点是你可以 yield 向协程运行器承诺并取回它们的结果,这样你就不必使用 then .

首先在您的 database.js 中创建 find 方法:

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;
    })
};