给定一个数组;如何 运行 第一个元素的函数并有条件地等待,return 或 运行 下一个元素的函数?
Given an array; how do I run a function for the first element and conditionally wait, return, or run the function for the next element?
我在 Parse Cloud 上有一组函数可以查询我在外部有 运行 的 node/mongo API。
其中一个函数为我提供了一组活动用户 objectId,每个都映射到按距离排序的消息通道名称。
我需要获取这个 objectId 数组并从第一个元素开始:
- 发送消息;
- 设置超时(我会在测试时尝试 30 秒,但在产品中可能会更长);
- 如果在超时期间我收到 'unavailable' 响应,则放弃剩余时间,继续下一个元素并返回 1;
- 如果在超时期间我收到 'available' 响应,则退出循环,忘记所有其他元素并 return 成功。
- 如果我完全没有收到任何回复,return 失败。
我不确定 Parse CloudCode 是否适合这项工作,所以我标记了节点以防我需要将逻辑放在 API 中。
JS 在这方面还很年轻,所以有一些深度的答案或让我学到一些可以回答我的问题的东西会受到赞赏和支持。
这是控制流的问题,一个选项是 async
包或 Promise 库。
让我们看看异步库。我们可以使用 detectSeries 函数来解决这个问题。
//Might need this, if you need to stop the processing when it wasn't really a success,
//since detectSeries doesn't handle errors
var specialFlag = false;
function doYourLogic( item, callback ){
sendMessage( item.whatever, function( err, result ){
if( result.status === 'available' ){
//flag this item as the one that worked. This will break the detectSeries
callback(true);
}
//whether any other error, just move on to the next
callback(false);
}
}
//Iterate over the entire collection, running the function 'doYourLogic' on each item,
//where 'finalDetect' is called once everything is done
//(either a single item passed the test, or all failed and the result is undefined)
async.detectSeries( collection, doYourLogic,
function finalDetect( result ){
if( result === undefined ){
//none of the items returned true, so nothing passed
}
//the result is the item that was successful
var usefaulData = result.field1;
}
我们可以使用其他一些异步函数来解决这个问题,但该包的想法是管理所有这些异步调用的控制流 - 并确保它们按顺序发生。 Promise 库也可能有帮助,但目前没有必要,而且可能不值得麻烦。
我在 Parse Cloud 上有一组函数可以查询我在外部有 运行 的 node/mongo API。
其中一个函数为我提供了一组活动用户 objectId,每个都映射到按距离排序的消息通道名称。
我需要获取这个 objectId 数组并从第一个元素开始:
- 发送消息;
- 设置超时(我会在测试时尝试 30 秒,但在产品中可能会更长);
- 如果在超时期间我收到 'unavailable' 响应,则放弃剩余时间,继续下一个元素并返回 1;
- 如果在超时期间我收到 'available' 响应,则退出循环,忘记所有其他元素并 return 成功。
- 如果我完全没有收到任何回复,return 失败。
我不确定 Parse CloudCode 是否适合这项工作,所以我标记了节点以防我需要将逻辑放在 API 中。
JS 在这方面还很年轻,所以有一些深度的答案或让我学到一些可以回答我的问题的东西会受到赞赏和支持。
这是控制流的问题,一个选项是 async
包或 Promise 库。
让我们看看异步库。我们可以使用 detectSeries 函数来解决这个问题。
//Might need this, if you need to stop the processing when it wasn't really a success,
//since detectSeries doesn't handle errors
var specialFlag = false;
function doYourLogic( item, callback ){
sendMessage( item.whatever, function( err, result ){
if( result.status === 'available' ){
//flag this item as the one that worked. This will break the detectSeries
callback(true);
}
//whether any other error, just move on to the next
callback(false);
}
}
//Iterate over the entire collection, running the function 'doYourLogic' on each item,
//where 'finalDetect' is called once everything is done
//(either a single item passed the test, or all failed and the result is undefined)
async.detectSeries( collection, doYourLogic,
function finalDetect( result ){
if( result === undefined ){
//none of the items returned true, so nothing passed
}
//the result is the item that was successful
var usefaulData = result.field1;
}
我们可以使用其他一些异步函数来解决这个问题,但该包的想法是管理所有这些异步调用的控制流 - 并确保它们按顺序发生。 Promise 库也可能有帮助,但目前没有必要,而且可能不值得麻烦。