Tuesday 16 May 2017

Node.js async iteratee with timeout

Bit new to node.js and javascript. Attempting to have my foreach call a function that in turn does a remote call. I'd like there to be a delay between each but can't seem to work out where to put the set timeout.

I know I have my setTimeout in the wrong place below but putting it in there as an example.

   var likeRecommendation = function (recommendation, callback) {
            setTimeout(function () {
                context.Client.like(recommendation._id, function (error, data) {
                    recommendation['drupal_user_uid'] = context.message.uid;
                    recommendation['drupal_user_uuid'] = context.message.uuid;
                    console.log(recommendation);
                    if (error) return done(new Error('Could not like recommendations'));
                    context.broker.publish('saves_swipes_publication', recommendation, function (err, publication) {
                        if (err) throw err
                        publication.on('error', console.error);
                    });
                    console.log('Liked!');
                    return callback()
                });
            }, 2000);
        }

        async.forEach(context.recommendations, likeRecommendation, function (error) {
            if (!error) return done(null);
            done(new Error('Could not like recommendations'));
        });



via Scott Hooker

No comments:

Post a Comment