简体   繁体   English

从异步函数返回变量

[英]returning a variable form an async function

I have a module with a function which generates the value for a vaariable for a variable "stitcheBook". 我有一个模块,该模块的功能为变量“ stitcheBook”生成变量的值。 I can see and use this value using a callback. 我可以通过回调看到并使用此值。

However, I want to have this value available to me as a module property. 但是,我想将此值作为模块属性提供给我。 How can i achieve this? 我怎样才能做到这一点?

Note: I wish the output of the _BookStitcher.stitchAllStories function to go into the _BookStitcher.stitchedBook property. 注意:希望_BookStitcher.stitchAllStories函数的输出进入_BookStitcher.stitchedBook属性。

module.exports = _BookStitcher = (function() {

var db = require('../modules/db');
var stitchedBook = {};

var stitchAllStories = function(callback) {


    db.dbConnection.smembers("storyIdSet", function (err, reply) {
        if (err) throw err;
        else {
            var storyList = reply;
            console.log(storyList);
            // start a separate multi command queue
            multi = db.dbConnection.multi();
            for (var i=0; i<storyList.length; i++) {
                multi.hgetall('story/' + String(storyList[i]) + '/properties');
            };
            // drains multi queue and runs atomically
            multi.exec(function (err, replies) {
                stitchedBook = replies;
                // console.log(stitchedBook);
                callback(stitchedBook);
            });
        };
    });


};


return {
    stitchedBook : stitchedBook,
    stitchAllStories: stitchAllStories

}

})(); })();

EDIT: to add: I know that I can actually set the value from outside by doing something like this; 编辑:添加:我知道我实际上可以通过执行类似这样的操作从外部设置值;

_BookStitcher.stitchAllStories(function (reply) {
        console.log("Book has been stitched!\n\n")
        console.log("the Book is;\n");
        console.log(reply);
        _BookStitcher.stitchedBook = reply;
        console.log("-------------------------------------------------------------------------\n\n\n");
        console.log(_BookStitcher.stitchedBook);

});

I was wondering if there was a way of doing it from inside the _BookStitcher module itself. 我想知道是否有办法从_BookStitcher模块本身内部进行操作。

You could take advantage of how object references work in JavaScript, and assign it to a property: 可以利用对象引用在JavaScript中的工作方式,并将其分配给属性:

module.exports = _BookStitcher = (function() {

    var db = require('../modules/db');

    // CHANGE HERE
    var stitched = { book: null };

    var stitchAllStories = function(callback) {
        db.dbConnection.smembers("storyIdSet", function (err, reply) {
            if (err) throw err;
            else {
                var storyList = reply;
                console.log(storyList);
                // start a separate multi command queue
                multi = db.dbConnection.multi();
                for (var i=0; i<storyList.length; i++) {
                    multi.hgetall('story/' + String(storyList[i]) + '/properties');
                };
                // drains multi queue and runs atomically
                multi.exec(function (err, replies) {
                    // CHANGE HERE
                    stitched.book = replies;
                    // console.log(stitchedBook);
                    callback(replies);
                });
            };
        });
    };

    return {
        stitched : stitched,
        stitchAllStories: stitchAllStories
    };

}());

So instead of having it inside _BookStitcher.stitchedBook , you'd have it at _BookStitcher.stitched.book . 因此,您可以将它放在_BookStitcher.stitchedBook ,而不是放在_BookStitcher.stitched.book

But that looks awful, and I'd never use it! 但这看起来很糟糕,我永远不会使用它! You can't know when the value will be available, it's only safe to use it from the callback, when you're sure it's been set. 您无法确定何时可以使用该值,只有在确定已设置该值的情况下,才可以从回调中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM