我试图在异步API的循环内执行多个调用,直到返回我想要的值(在此例中为true
)。问题是我不希望在找到该值后继续循环,并且我不想在循环之后执行循环,而没有从它返回的值...但是到目前为止,我无法实现这个工作。我认为我在这里做错了什么,但是“我无法弄清楚整个情况”。在循环内进行异步调用,直到找到值
function isSelected(element, callback) {
// This is a little bit confusing for me...can I just get the value from
// getDataAsync without using isSelected function?
Office.select(element).getDataAsync(function (asyncResult) {
var result = true;
// some logic here that may change 'result'
callback(result);
});
}
function delete(elements) {
var index = -1;
for (var i = 0, (i < elements.length) && (index < 0); i++) {
isSelected(elements[i], function(result) {
if (result) { index = i; }; // ...and I don't want to keep "looping"
});
}
// I want to execute these lines after the for-loop is done
// ...do something with the "correct" index value
}
为什么不选择“接受答案”缺少一些东西? – Aminadav
@Amina这里有个条件:'if(result){index = i; }'...之后,我不能再执行了,否则我可能会得到更多'真正'的值 –