2

I have a for loop that needs to return something on every iteration:

for(var i=0;i<100;++i) {
 return i;
}

but return breaks the loop. How can I return but kee the loop running?

asked May 15, 2013 at 12:57
4
  • 1
    What do you expect it to return, then? Commented May 15, 2013 at 12:59
  • 2
    You can't. What you can do however, is push the return value to an array for example. Can you be more specific on what you want to achieve? Commented May 15, 2013 at 12:59
  • 1
    Depending on your target system, yield could be a solution (so-question). Commented May 15, 2013 at 13:02
  • Return something to where? By definition a function only returns one "thing", though that "thing" may be an array or object that contains multiple values. (Unless yield works for your situation.) Commented May 15, 2013 at 13:13

4 Answers 4

13

Store it inside an array.

var array = [];
for (var i = 0; i < 100; ++i) {
 array.push(i);
}
answered May 15, 2013 at 12:59
Sign up to request clarification or add additional context in comments.

3 Comments

@Doorknob This is not Java.
thought as much. I can't use an array in this instance.
@MikeRifgin Then what do you want it to return?
5

The context here is unclear. If you are trying to execute a function for each iteration of the loop, why not something simple like calling a function from within the loop?

for(var i=0;i<100;++i) {
 processIteration(i)
}

In processIteration you could simply handle what you need to happen with that value.

answered May 15, 2013 at 13:00

Comments

2

Store the values you need to return in an array, then after the loop, return the array.

answered May 15, 2013 at 12:59

Comments

0

There are 3 ways to solve this

  1. Promises

function loop() {
 promises = []
 for (var i = 0; i < 100; ++i) {
 // push a promise resolve to a promises array
 promises[i] = Promise.resolve(i);
 }
 return promises;
}
// Observe the promises loop
for (let p of loop()) {
 p.then(console.log)
}

  1. Callbacks

function loop(cb) {
 for(var i = 0; i < 100; ++i) {
 // calling the callback 
 cb(i);
 }
}
// Pass a do something method
// Here it's the console log as the callback
loop(console.log)

  1. Yield

// Generator function for loop over 100
function* loop() {
 for (var i = 0; i < 100; ++i) {
 // return value from loop 
 yield i;
 }
}
// Initialize the generator function
let it = loop();
// Call and get the resulr
// it.next().value
var result = it.next();
while (!result.done) {
 console.log(result.value); // 1 2 3 .... 99
 result = it.next();
}

answered Aug 15, 2018 at 5:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.