Strange :-)

On Tue, Mar 20, 2012 at 4:02 PM, Angelo Chen <[email protected]> wrote:
> this works, here is the final version, thanks:
>
> exports.get_list = function (cb) {
>  rclient.smembers("list", function (err, members) {
>    var len = members.length
>    members.forEach(function (member) {
>      console.log(member)
>      rclient.hgetall(member, function (err, item) {
>        console.log(item)
>        len --
>        if (len === 0)
>           cb(null, 'lst')
>      })
>    })
>  })
> }
>
>
> On Mar 20, 9:03 pm, Oliver Leics <[email protected]> wrote:
>> > since forEach is a synchronous as you pointed out, cb got called at
>> > end of the loop, that's what I was expecting, but when every iteration
>> > also calls an asynchronous callback, that will be different as those
>> > asynchronous calls in fact happens after the cb(null, 'lst'), how to
>> > deal with this kind of situation?
>>
>> Get the definite number of comming items:
>>
>> exports.get_list = function (cb) {
>>   rclient.smembers("list", function(err, members) {
>>     if(err) { return cb(err) }
>>     if(members.length==0) return cb(null, 'lst');
>>     members.forEach(function(member){
>>       console.log(member)
>>
>>       var pending = rclient.hnumall(member);
>>       if(!pending) cb(null, 'lst')
>>
>>       rclient.hgetall(member, function(err, item) {
>>         if(err) { return cb(err) }
>>         console.log(item)
>>         if(!(--pending)) cb(null, 'lst')
>>       })
>>     })
>>   })
>>
>> }
>>
>> Or use an second callback on rclient.hgetall and call it at the end of
>> iteration:
>>
>> rclient.hgetall(member, cb, cbDone)
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: 
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en



-- 
Oliver Leics @ G+
https://plus.google.com/112912441146721682527

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to