Pergunta

How can i initialize async.queue "for" the global module scope? The example bellow shows the main problem, that qq is undefined, not yet known or its only defined locally in a function scope.

The target is to access a "module-global" q in different module member functions. So creating a module pattern version of the example in https://github.com/caolan/async#queue

I know why the // not working-code isn't valid, it is only too show which declaration ideas i tried.

Additional i m aware of how to solve the problem by using a different pattern, but that wouldn't answer the question.

 var mymodule = (function() {
    'use strict';

    var async = require('async');
    // var  q = async.queue(mymodule.qq); // not working 
    // var q ; // not working 
    var mymodule = {

        // q = async.queue(this.qq); // not working
        init: function() {
            // var q = async.queue(this.qq); // local not global
            // q = async.queue(this.qq); // not working
            q.drain = function() {
                console.log('all items have been processed');
            }
        },

        add: function(task) {
            this.q.push(task);
        },

        qq: function(task, callback) {
            console.log(task);
            callback();
        },

    };
    return mymodule;
 }());
Foi útil?

Solução

'use strict';
var async = require('async');
var mymodule = function(){

//This will be you constructor
//You can do something like this
  this.queue = async.queue(function(task, callback){
    console.dir(task);
  }, 4);
};
//Now start adding your methods
mymodule.prototype.add = function(task){
  this.queue.push(task, function(){});
};

mymodule.prototype.qq = function(task, callback){
// ..
callback()
};
//export it

module.exports = mymodule;
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top