1

I have this function:

function tryStartLocalTrendsFetch(woeid) {
    var userIds = Object.keys(twitClientsMap);
    var isStarted = false;

    for (var i = 0; i < userIds.length; i++) {
        var userId = userIds[i];
        var twitClientData = twitClientsMap[userId];
        var isWoeidMatch = (woeid === twitClientData.woeid);
        if (isWoeidMatch) {

            startLocalTrendsFetch(woeid, twitClientData, function (err, data) {
                if (err) {
                    // Couldn't start local trends fetch for userId: and woeid:
                    isStarted = false;
                } else {
                    isStarted = true;
                }
            });
            // This will not obviously work because startLocalTrendsFetch method is async and will execute immediately
            if (isStarted) {
                break;
            }
        }
    }
    console.log("No users are fetching woeid: " + woeid);
}

The gist of this method is that I want the line if (isStarted) { break; } to work. The reason is that if it's started it should not continue the loop and try to start another one.

I'm doing this in NodeJS.

3
  • 2
    Try looking up the async Node package on NPM. Commented Jun 24, 2015 at 13:51
  • I'd make i an index variable inside the loop, and change that outer 'for' loop into a while(!isStarted && i<userIds.length). When isStarted=true, the loop falls through and eliminates the need for the 'break'; however, I think there may be other issues with this structure... Commented Jun 24, 2015 at 13:54
  • If you're up to refactor your code to avoid callbacks, what @Scimonster said, async, will work. I'd recommend Promises, bluebird is great. My strongest recommendation would be FRP with Kefir. Commented Jun 24, 2015 at 13:56

2 Answers 2

2

try to use a recursive definition instead

function tryStartLocalTrendsFetch(woeid) {
  var userIds = Object.keys(twitClientsMap);
  recursiveDefinition (userIds, woeid);
}

function recursiveDefinition (userIds, woeid, userIndex)
  var userId = userIds[userIndex = userIndex || 0];
  var twitClientData = twitClientsMap[userId];
  var isWoeidMatch = (woeid === twitClientData.woeid);
  if (isWoeidMatch && userIndex<userIds.length) {
      startLocalTrendsFetch(woeid, twitClientData, function (err, data) {
          if (err) {
            recursiveDefinition(userIds, woeid, userIndex + 1)
          } else {
            console.log("No users are fetching woeid: " + woeid);
          }
      });
  } else {
    console.log("No users are fetching woeid: " + woeid);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah smart, I was thinking a recursive method might do the job here :)
1

You may also use async (npm install async):

var async = require('async');

async.forEach(row, function(col, callback){
    // Do your magic here
    callback();  // indicates the end of loop - exit out of loop
    }, function(err){
        if(err) throw err;
    });

More material to help you out: Node.js - Using the async lib - async.foreach with object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.