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.

Rishav Sharan
  • 2,763
  • 8
  • 39
  • 55
  • possible duplicate of [JavaScript asynchronous return value / assignment with jQuery](http://stackoverflow.com/questions/7779697/javascript-asynchronous-return-value-assignment-with-jquery) – jbabey Jan 31 '13 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. – bfavaretto Jan 31 '13 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. – Rishav Sharan Jan 31 '13 at 20:54

1 Answers1

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.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150