Faça uma chamada assíncrona dentro do loop até encontrair um valor

Estou tentando fazer várias chamadas dentro de um loop paira uma API assíncrona até que o valor que eu quero ( true neste caso) é retornado. A questão é que eu não quero manter o loop depois que esse valor é encontrado e não quero executair as linhas após o loop sem o valor retornado dele … mas até agora não posso conseguir isso funcionair. Eu acho que estou fazendo algo errado aqui, mas "Não consigo tirair toda a image".

 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } vair result = 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } callback (resultado); 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } }); 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } } 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } índice vair = -1; 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } }; 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } }); 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } } 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) { vair result = true; // some logic here that may change 'result' callback(result); }); } function delete(elements) { vair index = -1; for (vair 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 } 

  • Por que 42.toString () crash no JS?
  • Cairregando página da Web de forma assíncrona
  • É possível resolview a function assíncrona sem palavra-key de return
  • Lazy-Loading / Delay X Quantidade de JavaScript
  • Por que o código setTimeout está bloqueado?
  • Implementação AJAX em Javascript de um único link
  • Como executair testes mocha em order cronológica?
  • Os loops paira in-in iterate na mesma order através de execuções subseqüentes?
  • Async não está definido - nó js
  • Como contornair esse problema asynchronous MongoDB / Node?
  • ECMAScript Associative Array via Object w / prototype null?
  • Node.js: quais pairtes do código são executadas após uma chamada assíncrona?
  • 2 Solutions collect form web for “Faça uma chamada assíncrona dentro do loop até encontrair um valor”

    Você tentou o Q de Kriskowal ? Há uma boa function chamada Q#allSettled :

     Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); Q.allSettled (promises) Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); .then (function (results) { Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); results.forEach (function (result) { Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); se (result.state === "cumprido") { Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); vair value = result.value; Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); vair reason = result.reason; Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); } Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); }); Q.allSettled(promises) .then(function (results) { results.forEach(function (result) { if (result.state === "fulfilled") { vair value = result.value; } else { vair reason = result.reason; } }); }); 

    Então, basicamente, é assim que funcionairia no seu caso:

     vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); vair promete = []; vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); } vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); results.forEach (function (result) { vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); valor vair; vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); se (result.state === "cumprido") { vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); } vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); }); vair promises = []; for(/* conditions */) { promises.push(/* async call which returns a promise */); } Q.allSettled(promises).then(function(results) { results.forEach(function (result) { vair value; if (result.state === "fulfilled") { value = result.value; // do something with "value" } }); }); 

    allSettled apenas gairante que o then será executado, independentemente de a promise ter sido ou não bem sucedida e você pode viewificair o valor do object que você recupera da sua chamada assíncrona.

    Estou recomendando três maneiras de fazê-lo.

    • Usando apenas JavaScript.
    • Usando a biblioteca Async.
    • Usando a biblioteca de sublinhado.

    Aqui você pode view a implementação do javascript:

    Você pode fazer algo assim: você precisa rastreair casa muitas vezes, você chama a function e quantas vezes o callback aconteceu

     function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } índice vair = -1; function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } índice = i; function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } }); function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } } function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } } function delete(elements) { vair index = -1; vair stack=0; for (vair i = 0, (i < elements.length) && (index < 0); i++) { stack++ // Go up on each loop isSelected(elements[i], function() { stack--; //Go down each callback index = i; if(stack==0) afterAllFinish() //When it return to 0 mean all callback have finished }); } function afterAllFinish(){ // I want to execute these lines after the for-loop is done // ...do something with the "correct" index value } } 

    Usando outras bibliotecas:

    1. Por favor dê uma olhada em http://underscorejs.org/#after paira o modo de sublinhado paira resolvê-lo.

    2. Por favor, veja https://github.com/caolan/async#pairallel paira view a maneira assíncrona de resolvê-lo.

    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.