0

I am a newbie in node just trying out an example on the usage of Q. I am not able to get the log 'addition completed' printed. I am getting 'inside 7' & '7' in the output. What am I doing wrong?

My code is

var Q = require('q');

function Add(callback){

    setTimeout(function(){
        var a, b, c;
        b = 3; 
        a = 4;
        c = a + b; 
        console.log("inside "+c);
        callback(c);
    }, 2000); 

}

var promise = Q.denodeify(Add);

function callback(x){
    console.log(x);
}

promise(callback).then(function(){
    console.log('addition completed');
});
Olyve
  • 701
  • 1
  • 8
  • 27

3 Answers3

1

This sample code won't answer any questions you might have about the q library.
Instead, it just demonstrates how you can use a promise instead of a callback to complete an asynchronous task -- which seems like the underlying goal of your code.

(For more info and examples, you can check out MDN: Promises)

Note that promises are an alternative to callbacks, so an expression like promise(callback) is not something you'd typically write.

Also, I only looked at q very briefly, but I think Q.denodeify might not be the correct method to use for the task you're trying to accomplish.

For suggestions specific to q, you might also find this answer helpful.

function add(){

  // Calling `add` will return a promise that eventually resolves to `7`
  // There is no way this particular promise can reject (unlike most promises)
  const myPromise = new Promise(function(resolve, reject) {

    // The anonymous function we pass to `setTimeout` runs asynchronously
    // Whevever it is done, it applies the promise's `resolve` function to `c`
    setTimeout(function(){
      const
        a = 4,
        b = 3,
        c = a + b;
      resolve(c);
    }, 200);
  });
  return myPromise;
}

// `add` returns a promise that always resolves to `7`
// The `.then` method of this promise lets us specify a function that 
//   does something with the resolved value when it becomes available
add().then(function(resolvedValue){
  console.log("addition result: " + resolvedValue);
});
Cat
  • 4,141
  • 2
  • 10
  • 18
0

I tweaked it a bit and got it to work. But i am not able to understand clearly why this worked and the one prior to this failed. Hope someone call help with an explanation.

My new code is

var Q = require('q');

function Add(callback){

    setTimeout(function(){
        var a, b, c;
        b = 3; 
        a = 4;
        c = a + b; 
        console.log("inside "+ c);
        callback();

    }, 2000); 

}

var promise = Q.denodeify(Add);

promise().then(function(){
    console.log('addition completed');
});

0

you can use this example for the promise

 var Q = require('q');

    function add(callback){

        return new Promise(function ( resolve, reject) {
            setTimeout(function(){
                var a, b, c;
                b = 3;
                a = 4;
                c = a + b;
                console.log("inside "+c);
                callback(c);
                if(c){
                    resolve(c);
                }else{
                    reject();
                }
            }, 2000);

        });
    }

    //var promise = Q.nbind(add);

    function callback(x){
        console.log(x);
    }

    add(callback).then(function(){
        console.log('addition completed');
    }).catch(function (E) {
        console.log(E);
    });
ANIK ISLAM SHOJIB
  • 3,002
  • 1
  • 27
  • 36