complex collection of objects with async method

24 views
Skip to first unread message

pee...@gmail.com

unread,
Oct 15, 2013, 4:52:13 AM10/15/13
to q-con...@googlegroups.com
Hi,

I'm new to Q and promises. I'm trying to use it in my code - working with NodeJS. 

I have an object, objB, which has an async method called extract() that returns a promise (using Q.defer). extract() makes a http "get" request (hence the async nature) and when the request is successful, it updates the object's properties with the response and resolves the promise.

I have another object, objA, that contains an array of objB.

I want to iterate over a collection (array) of objA. For each objA in the array, I want to call the extract() method for only the first element in its objB array.

The issue I have is that it all works and extract is called, except the property update that extract also performs does not seem to happen.

//objA has property blist which is a collection of objB

//extract method of objB
ObjB.prototype.extract = function()
{
    var deferred = Q.defer();
    https.get(url, function(res) {
                           res.on('data', function(data) {
                                                   //do stuff with data
                                                   //update objB property
                                                   this._property1 = data.field1
                                                   deferred.resolve(data);
                                              });
                       };
    return deferred.promise;
};

I call this as:
//alist is the collection of objA
var queue = [];
for (var i=0; j<alist.length; i++)
{
var myfunc = alist[i].blist[0].extract.bind(alist[i].blist[0]);
queue.push(myfunc());
}

Q.all(queue).then(func(succ){
                              //print alist results

The issue I have is that _property1 of objB is not being updated. Extract is correctly called. But then when all the promises have been resolved and I output the original collection, alist, the property update that extract() did on objB is not reflected.

Instead of the nbind, I have tried: queue.push(Q(alist[i].blist[0]).invoke("extract")) which I saw had been suggested in this group earlier. That also does not work.

Any ideas what I am doing wrong? I'm guessing its something to do with the bind...

thanks

Stuart Knightley

unread,
Oct 15, 2013, 10:35:31 AM10/15/13
to q-con...@googlegroups.com

Javascript's 'this' is not lexically scoped, and so you need to keep a reference to it to use in callbacks (i.e. in ObjB extract). Often 'var self = this;'

See http://www.2ality.com/2012/04/arrow-functions.html for more information (although arrow functions aren't yet in node)

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

pee...@gmail.com

unread,
Oct 16, 2013, 4:36:59 AM10/16/13
to q-con...@googlegroups.com
Thanks! Thats exactly what it was.
Reply all
Reply to author
Forward
0 new messages