How To Break A For Loop Inside An Asynchronous Callback Function
I am trying to break a nested for loop inside a asynchronous call back but unable to do so:
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
function callApi()
//response from intial call
for (var key in response)
var data = response[key];
(function (innerData)
if (innerData)
renderHTML(innerData);
)(data);
function renderHTML(data)
for (var key in data)
var index = data[key];
(function (innerData, id)
asyncCall(id, function (isFound)
if (isFound)
break; //break loop
);
)(data, index);
callApi();
I want to break the loop if the value of the property isFound is true in its response and want to achieve this in ES5 only or any work around like synchronous call might help.
javascript ecmascript-5
add a comment |
I am trying to break a nested for loop inside a asynchronous call back but unable to do so:
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
function callApi()
//response from intial call
for (var key in response)
var data = response[key];
(function (innerData)
if (innerData)
renderHTML(innerData);
)(data);
function renderHTML(data)
for (var key in data)
var index = data[key];
(function (innerData, id)
asyncCall(id, function (isFound)
if (isFound)
break; //break loop
);
)(data, index);
callApi();
I want to break the loop if the value of the property isFound is true in its response and want to achieve this in ES5 only or any work around like synchronous call might help.
javascript ecmascript-5
1
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40
add a comment |
I am trying to break a nested for loop inside a asynchronous call back but unable to do so:
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
function callApi()
//response from intial call
for (var key in response)
var data = response[key];
(function (innerData)
if (innerData)
renderHTML(innerData);
)(data);
function renderHTML(data)
for (var key in data)
var index = data[key];
(function (innerData, id)
asyncCall(id, function (isFound)
if (isFound)
break; //break loop
);
)(data, index);
callApi();
I want to break the loop if the value of the property isFound is true in its response and want to achieve this in ES5 only or any work around like synchronous call might help.
javascript ecmascript-5
I am trying to break a nested for loop inside a asynchronous call back but unable to do so:
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
function callApi()
//response from intial call
for (var key in response)
var data = response[key];
(function (innerData)
if (innerData)
renderHTML(innerData);
)(data);
function renderHTML(data)
for (var key in data)
var index = data[key];
(function (innerData, id)
asyncCall(id, function (isFound)
if (isFound)
break; //break loop
);
)(data, index);
callApi();
I want to break the loop if the value of the property isFound is true in its response and want to achieve this in ES5 only or any work around like synchronous call might help.
javascript ecmascript-5
javascript ecmascript-5
edited Nov 12 '18 at 17:10
Gary Thomas
1,1081312
1,1081312
asked Nov 12 '18 at 15:28
Asad ShahAsad Shah
179112
179112
1
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40
add a comment |
1
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40
1
1
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40
add a comment |
2 Answers
2
active
oldest
votes
You can't.
The loop will have finished before the break gets reached.
The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).
"The only way to do this would be to run each invocation ofasyncCallin series instead of in parallel". Can you show it with the help of some example ?
– Asad Shah
Nov 13 '18 at 6:38
add a comment |
As Quentin said, you can't.
But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.
let isFound = false;
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
if(isFound) return;
isFound = true;
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit theonSuccessfunction? or the api?
– jacksbox
Nov 15 '18 at 9:34
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265283%2fhow-to-break-a-for-loop-inside-an-asynchronous-callback-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't.
The loop will have finished before the break gets reached.
The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).
"The only way to do this would be to run each invocation ofasyncCallin series instead of in parallel". Can you show it with the help of some example ?
– Asad Shah
Nov 13 '18 at 6:38
add a comment |
You can't.
The loop will have finished before the break gets reached.
The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).
"The only way to do this would be to run each invocation ofasyncCallin series instead of in parallel". Can you show it with the help of some example ?
– Asad Shah
Nov 13 '18 at 6:38
add a comment |
You can't.
The loop will have finished before the break gets reached.
The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).
You can't.
The loop will have finished before the break gets reached.
The only way to do this would be to run each invocation of asyncCall in series instead of in parallel. (e.g. by calling the next one from the callback function passed to the previous one).
answered Nov 12 '18 at 15:33
QuentinQuentin
643k718671036
643k718671036
"The only way to do this would be to run each invocation ofasyncCallin series instead of in parallel". Can you show it with the help of some example ?
– Asad Shah
Nov 13 '18 at 6:38
add a comment |
"The only way to do this would be to run each invocation ofasyncCallin series instead of in parallel". Can you show it with the help of some example ?
– Asad Shah
Nov 13 '18 at 6:38
"The only way to do this would be to run each invocation of
asyncCall in series instead of in parallel". Can you show it with the help of some example ?– Asad Shah
Nov 13 '18 at 6:38
"The only way to do this would be to run each invocation of
asyncCall in series instead of in parallel". Can you show it with the help of some example ?– Asad Shah
Nov 13 '18 at 6:38
add a comment |
As Quentin said, you can't.
But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.
let isFound = false;
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
if(isFound) return;
isFound = true;
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit theonSuccessfunction? or the api?
– jacksbox
Nov 15 '18 at 9:34
add a comment |
As Quentin said, you can't.
But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.
let isFound = false;
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
if(isFound) return;
isFound = true;
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit theonSuccessfunction? or the api?
– jacksbox
Nov 15 '18 at 9:34
add a comment |
As Quentin said, you can't.
But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.
let isFound = false;
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
if(isFound) return;
isFound = true;
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
As Quentin said, you can't.
But if you want to prevent further callbacks from happening, you could set a variable which prevents the callbacks to happen.
let isFound = false;
function asyncCall(id, OnComplete)
// id..
context.executeQueryAsync(OnSuccess, OnFailure);
function OnSuccess(sender, args)
if(isFound) return;
isFound = true;
OnComplete(userInGroup);
function OnFailure(sender, args)
console.error("Doesn't Exist!")
answered Nov 12 '18 at 15:34
jacksboxjacksbox
6021720
6021720
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit theonSuccessfunction? or the api?
– jacksbox
Nov 15 '18 at 9:34
add a comment |
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit theonSuccessfunction? or the api?
– jacksbox
Nov 15 '18 at 9:34
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
At the moment i am doing exactly what you said, but don't want unnecessary calls to hit.
– Asad Shah
Nov 13 '18 at 10:45
hit the
onSuccess function? or the api?– jacksbox
Nov 15 '18 at 9:34
hit the
onSuccess function? or the api?– jacksbox
Nov 15 '18 at 9:34
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265283%2fhow-to-break-a-for-loop-inside-an-asynchronous-callback-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can't with the structure you show unless you make the asyc calls synchronous. As shown, the entire loop will probably have fully ran before the first asyc call resolves.
– Shilly
Nov 12 '18 at 15:33
This looks like Javascript breaking a for loop inside a callback function
– Heretic Monkey
Nov 12 '18 at 17:22
@Shilly thanks for the knowledge, how can i call this asynchronous method synchronously then?
– Asad Shah
Nov 13 '18 at 7:09
@AsadShah Instead of caliing asyncCall inside a loop, wait for the response to arrive and then call the next one. But depending on what executeQueryAsync does, you might be able to leave it async. Need more information for that thoguh.
– Shilly
Nov 13 '18 at 9:22
@Shilly executeQueryAsync takes id as parameter and fetches data from the server and returns boolean if the user exists in a group or not but i have to leave it async as it is the function of third party library. calling function asyncCall sequentially will help but don't know exactly how to call it sequentially.
– Asad Shah
Nov 13 '18 at 10:40