0

I am attempting to get the return value of a function.

I am doing it like this:

  let barHeight;
  componentDidMount() {
    barHeight = StatusBarManager.getHeight(statusBarHeight => statusBarHeight);

    console.log({ mounted: barHeight });
  }

  componentDidUpdate() {
    barHeight = StatusBarManager.getHeight(statusBarHeight => statusBarHeight);

    console.log({ update: barHeight });
  }

The curious thing is that if I do:

StatusBarManager.getHeight(statusBarHeight => console.log(statusBarHeight));

I see how it logs {height: 20}, but with both console logs inside those 2 methods: console.log({ update: barHeight }); all I see is undefined.

So how can I grab the value returned by StatusBarManager.getHeight in order to use it in another function?

Reacting
  • 5,543
  • 7
  • 35
  • 86
  • can you please add `StatusBarManager.getHeight` function – Aziz.G Mar 06 '19 at 19:28
  • 1
    `let` defines a scoped variable for the brackets. use `var barHeight` – Rafael Herscovici Mar 06 '19 at 19:28
  • 1
    @Dementic that isnt the problem. The problem is that you aren't waiting for a response to return. You have to `console.log` within the callback of `StatusBarManager.getHeight()` – Isaac Vidrine Mar 06 '19 at 19:30
  • @IsaacVidrine that would be true if it was a promise, nothing indicates such though. – Rafael Herscovici Mar 06 '19 at 19:32
  • @Dementic the fact that the OPs `console.logs` are undefined tells you that `.getHeight` is an async call – Isaac Vidrine Mar 06 '19 at 19:32
  • @IsaacVidrine so by that logic, `console.log(x)` X = Promise? X will be undefined, no matter if its a promsie or not. – Rafael Herscovici Mar 06 '19 at 19:33
  • @Dementic im saying that the value of `barHeight` is undefined because OP is logging it out before `.getHeight()` returns with a value. – Isaac Vidrine Mar 06 '19 at 19:39
  • 2
    @Dementic Just because it doesn't return a promise doesn't mean it's not an asynchronous call that has to be handled appropriately. The fact that the `getHeight()` method takes a callback (and evidently doesn't `return` anything) strongly suggest that it is async. – Lennholm Mar 06 '19 at 19:44

2 Answers2

0

console.log always returns undefined.You can use following code

StatusBarManager.getHeight(statusBarHeight => console.log(statusBarHeight) || statusBarHeight);
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • I did this but still can't use the value of `statusBarHeight `. I mean I can not grab that value. – Reacting Mar 06 '19 at 19:32
  • @Reacting Ok now check it. – Maheer Ali Mar 06 '19 at 19:33
  • @Reacting I think i misunderstood your question. Do you mean that how can you get the value of `statusBarHeight` and also `console.log()` it. – Maheer Ali Mar 06 '19 at 19:38
  • No, the console.log doesn't matter. What I am saying is that why I get the value `statusBarHeight` on the console.log but not from the value that function returned. – Reacting Mar 06 '19 at 19:41
  • All I want is the value returned by the function, in this case -> `statusBarHeight`. The `console.log` within the callback is just to show you that I can see the value on thast console.log within there, but it is `undefined` if I remove that console.log. – Reacting Mar 06 '19 at 19:42
  • @Reacting you aren't returning anything from that function – VLAZ Mar 06 '19 at 19:42
  • @Reacting the value/response from `.getHeight()` is only available within the callback, so to do anything with it, you have to do it inside its callback – Isaac Vidrine Mar 06 '19 at 19:54
  • @VLAZ, see my code, I am returning this -> `StatusBarManager.getHeight(statusBarHeight => statusBarHeight);` but the value is `undefined`. – Reacting Mar 06 '19 at 20:14
  • @Reacting there is no `return` statement, so you are not *returning* anything. You are *assigning* to the `barHeight` variable which is in a shared scope. Two quite different things. – VLAZ Mar 06 '19 at 20:20
  • @Reacting `statusBarHeight => statusBarHeight` is just **your** callback where you simply return the same value it gets invoked with. This doesn't magically make `getHeight` return this value. – Lennholm Mar 06 '19 at 20:37
0

The reason your barHeight is undefined in your console.log is because the console.log is being executed before the value of barHeight is updated with the response from StatusBarManager.getHeight(). As you've already noticed, for the console.log to work, you have to move it into the callback of getHeight().

let barHeight;
componentDidMount() {
  barHeight = StatusBarManager.getHeight(statusBarHeight => {
    console.log({ mounted: statusBarHeight });
    barHeight = statusBarHeight;
  });
}
Isaac Vidrine
  • 1,568
  • 1
  • 8
  • 20