0

I have a module with a function which generates the value for a vaariable for a variable "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.

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.

3
  • possible duplicate of JavaScript asynchronous return value / assignment with jQuery Commented Jan 31, 2013 at 20:52
  • 1
    Aren't you already doing that in stitchedBook = replies;? The problem is, 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. Commented Jan 31, 2013 at 20:52
  • sorry. please see my edit. I can get the value. I was just wondering if i can set the value from inside the module. Commented Jan 31, 2013 at 20:54

1 Answer 1

1

You could take advantage of how object references work in JavaScript, and assign it to a property:

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.

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I probably wouldn't use it but I just had to know. -_-

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.