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