Website Is Fun
Website Is Fun

Reputation: 300

returning a function value inside an iterator without exiting in javascript

I am not sure if my title fits my question but here it goes.

I have an iterator runs every 1 second and a function that goes like this:

function getValue(val) {
  return val;
}

function test() {
  let x = 20;
  getValue(x)

  setTimeout(()=>{
    test()
  }, 1000)
}

export default test;

When the function getValue() is hit, the iterator stops because there is a return inside that function. How do I return the value without exiting stopping the iteration? Is there a way? Thanks a lot.


index.js (reactjs)

import test from './test.js';

componentDidMount() {
  test()
}

test.js

function getValue(val) {
  return val;
}

function test() {
  let x = 20;
  getValue(x)

  setTimeout(()=>{
    test()
  }, 1000)
}

export default test;

My Aim here is to pass/return the value 20 to my index.js

Upvotes: 0

Views: 224

Answers (3)

Bergi
Bergi

Reputation: 664589

My Aim here is to pass/return the value 20 to my index.js

For that, you don't need a getValue function. Just write

// test.js
export default function test() {
  let x = 20;
  setTimeout(test, 1000)
  return x; // <===
}

// index.js
import test from './test.js';
console.log("test() returned", test());

Notice that also in the timeout you will get that return value now, and can use it if you care

// test.js
export default function test() {
  let x = 20;
  setTimeout(() => {
    let res = test();
    console.log("test() in timeout returned", res);
  }, 1000)
  return x;
}

any other options to this like it can pass value to the client every 1sec?

For that, you want to use a callback that is passed as a parameter to test and can be called to pass the value of x where- and whenever you want - and as often as necessary:

// test.js
export default function test(callback) {
//                           ^^^^^^^^
  let x = 20;
  callback(x); // <===
  setTimeout(() => {
    test(callback);
  }, 1000);
}

// index.js
import test from './test.js';
function gotValue(val) {
  console.log("received ", val);
}
test(gotValue);
//   ^^^^^^^^

Upvotes: 1

Alex Young
Alex Young

Reputation: 4039

If you want to receive a value from test every interval, then you need to pass a callback function that will be executed each time. It is simply not possible to return a value from a function multiple times (without a generator function, but that's not worth worrying about now).

const test = (callback) => {
    let x = 20;
    return setInterval(() => callback(x), 1000);
}

// then from elsewhere
// the lambda function will be called with x as 20
// every time the interval fires
const unsub = test((x) => {
   // do something with x
   // unsubscribe when you are ready and the interval will be cleared
   if (condition) {
       clearInterval(unsub);
   }
});

Upvotes: 1

Darokrithia
Darokrithia

Reputation: 160

I don't fully understand your question.

You don't seem to iterate (loop) at all in your function.

My suggestion is either to return at the very end of your function, or to make your function have no return statement, and instead change a value of a var to the desired type. If you want to give multiple values as your return, simply on of the two above options with an array that contains all desired values.

Upvotes: 0

Related Questions