Best way to wait for .forEach() to complete
up vote
4
down vote
favorite
Sometimes I need to wait for a .forEach() method to finish, mostly on 'loader' functions. This is the way I do that:
$q.when(array.forEach(function(item)
//iterate on something
)).then(function()
//continue with processing
);
I can't help but feel that this isn't the best way to wait for a .forEach() to finish. What is the best way to do this?
javascript angularjs
add a comment |
up vote
4
down vote
favorite
Sometimes I need to wait for a .forEach() method to finish, mostly on 'loader' functions. This is the way I do that:
$q.when(array.forEach(function(item)
//iterate on something
)).then(function()
//continue with processing
);
I can't help but feel that this isn't the best way to wait for a .forEach() to finish. What is the best way to do this?
javascript angularjs
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
Sometimes I need to wait for a .forEach() method to finish, mostly on 'loader' functions. This is the way I do that:
$q.when(array.forEach(function(item)
//iterate on something
)).then(function()
//continue with processing
);
I can't help but feel that this isn't the best way to wait for a .forEach() to finish. What is the best way to do this?
javascript angularjs
Sometimes I need to wait for a .forEach() method to finish, mostly on 'loader' functions. This is the way I do that:
$q.when(array.forEach(function(item)
//iterate on something
)).then(function()
//continue with processing
);
I can't help but feel that this isn't the best way to wait for a .forEach() to finish. What is the best way to do this?
javascript angularjs
javascript angularjs
edited Jul 16 '16 at 1:47
Rob
11.4k82852
11.4k82852
asked Jul 16 '16 at 1:41
Jaap Weijland
6721819
6721819
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
forEach is not asynchronous, for example in this code:
array.forEach(function(item)
//iterate on something
);
alert("Foreach DONE !");
you will see the alert after forEach finished.
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
add a comment |
up vote
10
down vote
var foo = [1,2,3,4,5,6,7,8,9,10];
If you're actually doing async stuff inside the loop, you can wrap it in a promise ...
var bar = new Promise((resolve, reject) =>
foo.forEach((value, index, array) =>
console.log(value);
if (index === array.length -1) resolve();
);
);
bar.then(() =>
console.log('All done!');
);
add a comment |
up vote
0
down vote
The quickest way to make this work using ES6 would be just to use a for..of loop.
const myAsyncLoopFunction = async (array)
const allAsyncResults =
for (const item of array)
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
return allAsyncResults
Or you could loop over all these async requests in parallel using Promise.all() like this:
const myAsyncLoopFunction = async (array)
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
add a comment |
up vote
-3
down vote
No, it is blocking. Have a look at the specification of the algorithm.
So whatever the code which you put outside forEach will execute once foEach() is done.
array.forEach(function(item)
//iterate on something
)
// @TODO this code will be executed once above block is done.
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
forEach is not asynchronous, for example in this code:
array.forEach(function(item)
//iterate on something
);
alert("Foreach DONE !");
you will see the alert after forEach finished.
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
add a comment |
up vote
4
down vote
accepted
forEach is not asynchronous, for example in this code:
array.forEach(function(item)
//iterate on something
);
alert("Foreach DONE !");
you will see the alert after forEach finished.
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
forEach is not asynchronous, for example in this code:
array.forEach(function(item)
//iterate on something
);
alert("Foreach DONE !");
you will see the alert after forEach finished.
forEach is not asynchronous, for example in this code:
array.forEach(function(item)
//iterate on something
);
alert("Foreach DONE !");
you will see the alert after forEach finished.
answered Jul 16 '16 at 1:52
Ismail RBOUH
7,69711028
7,69711028
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
add a comment |
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
5
5
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
This is OK if there is no async processing inside the loop.
– jarmod
Apr 4 at 16:51
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
@jarmod you are absolutely right ;)
– Ismail RBOUH
Apr 4 at 17:09
add a comment |
up vote
10
down vote
var foo = [1,2,3,4,5,6,7,8,9,10];
If you're actually doing async stuff inside the loop, you can wrap it in a promise ...
var bar = new Promise((resolve, reject) =>
foo.forEach((value, index, array) =>
console.log(value);
if (index === array.length -1) resolve();
);
);
bar.then(() =>
console.log('All done!');
);
add a comment |
up vote
10
down vote
var foo = [1,2,3,4,5,6,7,8,9,10];
If you're actually doing async stuff inside the loop, you can wrap it in a promise ...
var bar = new Promise((resolve, reject) =>
foo.forEach((value, index, array) =>
console.log(value);
if (index === array.length -1) resolve();
);
);
bar.then(() =>
console.log('All done!');
);
add a comment |
up vote
10
down vote
up vote
10
down vote
var foo = [1,2,3,4,5,6,7,8,9,10];
If you're actually doing async stuff inside the loop, you can wrap it in a promise ...
var bar = new Promise((resolve, reject) =>
foo.forEach((value, index, array) =>
console.log(value);
if (index === array.length -1) resolve();
);
);
bar.then(() =>
console.log('All done!');
);
var foo = [1,2,3,4,5,6,7,8,9,10];
If you're actually doing async stuff inside the loop, you can wrap it in a promise ...
var bar = new Promise((resolve, reject) =>
foo.forEach((value, index, array) =>
console.log(value);
if (index === array.length -1) resolve();
);
);
bar.then(() =>
console.log('All done!');
);
answered Jul 16 '16 at 2:00
Rolando Benjamin Vaz Ferreira
1266
1266
add a comment |
add a comment |
up vote
0
down vote
The quickest way to make this work using ES6 would be just to use a for..of loop.
const myAsyncLoopFunction = async (array)
const allAsyncResults =
for (const item of array)
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
return allAsyncResults
Or you could loop over all these async requests in parallel using Promise.all() like this:
const myAsyncLoopFunction = async (array)
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
add a comment |
up vote
0
down vote
The quickest way to make this work using ES6 would be just to use a for..of loop.
const myAsyncLoopFunction = async (array)
const allAsyncResults =
for (const item of array)
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
return allAsyncResults
Or you could loop over all these async requests in parallel using Promise.all() like this:
const myAsyncLoopFunction = async (array)
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
add a comment |
up vote
0
down vote
up vote
0
down vote
The quickest way to make this work using ES6 would be just to use a for..of loop.
const myAsyncLoopFunction = async (array)
const allAsyncResults =
for (const item of array)
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
return allAsyncResults
Or you could loop over all these async requests in parallel using Promise.all() like this:
const myAsyncLoopFunction = async (array)
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
The quickest way to make this work using ES6 would be just to use a for..of loop.
const myAsyncLoopFunction = async (array)
const allAsyncResults =
for (const item of array)
const asnycResult = await asyncFunction(item)
allAsyncResults.push(asyncResult)
return allAsyncResults
Or you could loop over all these async requests in parallel using Promise.all() like this:
const myAsyncLoopFunction = async (array)
const promises = array.map(asyncFunction)
await Promise.all(promises)
console.log(`All async tasks complete!`)
answered Nov 9 at 20:58
Douglas Rosebank
17127
17127
add a comment |
add a comment |
up vote
-3
down vote
No, it is blocking. Have a look at the specification of the algorithm.
So whatever the code which you put outside forEach will execute once foEach() is done.
array.forEach(function(item)
//iterate on something
)
// @TODO this code will be executed once above block is done.
add a comment |
up vote
-3
down vote
No, it is blocking. Have a look at the specification of the algorithm.
So whatever the code which you put outside forEach will execute once foEach() is done.
array.forEach(function(item)
//iterate on something
)
// @TODO this code will be executed once above block is done.
add a comment |
up vote
-3
down vote
up vote
-3
down vote
No, it is blocking. Have a look at the specification of the algorithm.
So whatever the code which you put outside forEach will execute once foEach() is done.
array.forEach(function(item)
//iterate on something
)
// @TODO this code will be executed once above block is done.
No, it is blocking. Have a look at the specification of the algorithm.
So whatever the code which you put outside forEach will execute once foEach() is done.
array.forEach(function(item)
//iterate on something
)
// @TODO this code will be executed once above block is done.
answered Jul 16 '16 at 4:10
Suneet Bansal
1,9051613
1,9051613
add a comment |
add a comment |
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%2f38406920%2fbest-way-to-wait-for-foreach-to-complete%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