【发布时间】:2015-09-10 18:23:06
【问题描述】:
我有这个功能:
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);
}
这种方法的要点是我希望if (isStarted) { break; } 行工作。原因是如果它已经启动,它不应该继续循环并尝试启动另一个循环。
我在 NodeJS 中这样做。
【问题讨论】:
-
尝试在 NPM 上查找
async节点包。 -
我将 i 在循环内设为索引变量,并将外部“for”循环更改为 while(!isStarted && i
标签: javascript node.js for-loop asynchronous break