Returning a value from a jaascript function

Hi guys,

I am fairly new to Javascript and I am trying to return a value from a function and use that returned value in a different function. I am however having some problem with it.

To simplify the question I am trying to return at the line “return [1,1]” but I always get a return at “return [1]”. Any help will be appreciated. Thanks.

function test(){
  getTopStories(true).then(function (topIds){
    for(var i = 0; i < 10; i++){
      callFetch(topIds[i],i);
    }
    return [1,1];
    })
    .catch(error => {
     console.log('There was an error: ', error);
  });
    return [1];
}

The [1,1] is the return value for the anonymous function you’re passing as the callback for your then. It can’t be returned by your test function.

Your best bet is to modify your function to return the promise. Then, when you want to use the value elsewhere, use it in a promise chain like this:

test().then(function(returnValueOfTestFunction) {
  // whatever you want to do with the value returned by test
});

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.