# Determine if twitch stream is offline

**URL:** <https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395>\
**Category:** General\
**Created:** [May 5, 2018, 4:41am UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395 "2018-05-05T04:41:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 4:41am UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/1 "2018-05-05T04:41:47Z")

</div>

I’m currently doing Twitch api project. My problem is how to determine which streams are offline **AND** get their names, logos, urls, which i can’t if i use “[https://api.twitch.tv/kraken/streams/](https://api.twitch.tv/kraken/streams/)”+streamers[i] + “?client\_id=” + clientId. I checked if stream is null, then i can determine if they are online or offline, but cannot get info for streamers who are currently offline .

If i use “[https://api.twitch.tv/kraken/channels/](https://api.twitch.tv/kraken/channels/)”+streamers[i] + “?client\_id=” + clientId , i get all necessary info, but now cannot determine which are offline or online …

Here’s a link to my pen: [https://codepen.io/codename11/pen/PemrXN](https://codepen.io/codename11/pen/PemrXN)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 7:07am UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/2 "2018-05-05T07:07:42Z")

</div>

I seem to remember backing two JSON calls for each user.

---

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 7:33am UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/3 "2018-05-05T07:33:52Z")

</div>

At same time? Considering nature of calls(asinc) how can something be achieved? I tried nesting one asinc call within another, but that doesn’t seem to work for some reason … Is that you meant when you said “backing”?

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 2:37pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/4 "2018-05-05T14:37:04Z")

</div>

Sorry, I was tired when I was typing. I don’t know what I meant by backing. I meant either using or calling. I just made two separate calls.

---

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 2:54pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/5 "2018-05-05T14:54:15Z")

</div>

In meantime, i tried using $.when().then(). Structure of it: $.when() encompass $.each(array as parameter) which encompass $.getJSON from which should get json. This only works if i ditch $.each and in $.getJSON and instead of “this” keyword i use string from previously said arrays … Long story-short, $.when().then() works on single string and now i need it to work with array of strings. This is link to forked code: [https://codepen.io/codename11/pen/wjroLQ](https://codepen.io/codename11/pen/wjroLQ)

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 4:16pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/6 "2018-05-05T16:16:35Z")

</div>

There are different ways to handle this. I don’t see why you need _$.when()_, but that could work.

The big choice to me, is do we wait until all the data is back, store it in an array and _then_ write it to the screen, or do we write it as it comes in. To me the second option is a better UX so they can see the data come in. If you set up your table ahead of time, you should just be able to write the data as it comes in.

---

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 4:30pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/7 "2018-05-05T16:30:17Z")

</div>

Problem is that i need info from both of jsons since either of them doesn’t contain **ALL** info i need. As i said, i tried your second option first, but even if i declare arrays for both jsons as global variable, i cannot access them outside $.getJSON … Thus i need $.when().then() . If i do this, new problem occurs of how to incorporate $.each since i handle arrays of json requests and not single request.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 4:41pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/8 "2018-05-05T16:41:00Z")

</div>

Right, but using `id`s and `class`es you can target whatever grid cells you need. I’m telling you, you do not _need_ to wait for all the data. If you _want_ to that’s fine, but you don’t _need_ to.

> but even if i declare arrays for both jsons as global variable, i cannot access them outside $.getJSON

This is the difficulty/beauty of asynchronous coding. You _can_ access them as global variables outside the AJAX call, but the problem is _when_ you access them they probably aren’t defined yet. There are ways to handle them, but the easiest way is to put the code you need inside the callback or promise so it gets called _after_ the AJAX call. And if you choose to wait until all the data is back, then you can chain them with nested callbacks or with _then_ methods and put your output code in the second callback or _then_.

> Thus i need $.when().then()

No, you don’t. You _can_ do it that way, but you don’t _need_ to. Again, nesting callbacks or chaining promises will do the same thing. I’m not saying your approach is bad, but that you seem to incorrectly think it is your only option.

> If i do this, new problem occurs of how to incorporate $.each since i handle arrays of json requests and not single request.

Again, you can handle each request as it comes in. But you you want to create arrays of the data, you can do that too. Then you will presumably you will have two arrays. Inside your final _then_ you can loop them and output them.

---

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 5:16pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/9 "2018-05-05T17:16:39Z")

</div>

I did target them by id. I know how to do that. Problem is that i don’t have all info i need from just one json call, so i need them both. And i need them at same time, at the same place(id). Problem is how to call to jsons at parallel so i can target ids. I tried nesting callbacks, but inner callback cannot access outer callbacks data and i don’t know why and/or data is undefined …

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 6:01pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/10 "2018-05-05T18:01:27Z")

</div>

Again, you keep telling me that you _need_ to do it that way, and I keep telling you you don’t need to. [Here is an example of writing](https://codepen.io/kevinsmithwebdev/pen/jxwwPW) to the DOM asynchronously. The mechanics of the function may be beyond your ken at the moment, but the idea is what matters - using jQuery to selectively target cells as the data is available.

Again, this is not the _only_ way to do it, but it is a good way and I think is better for UX.

> I tried nesting callbacks, but inner callback cannot access outer callbacks data and i don’t know why and/or data is undefined …

That is not true. An inner nested callback should have the outer callback in scope. A while back I wrote [this pen](https://codepen.io/kevinsmithwebdev/pen/jLENXP?editors=0010) to help people understand async behavior. It is an example of nested callbacks but it would work with promises too.

---

<div class="post-metadata">

**Author:** ![codename11](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codename11/32/85412_2.png) [@codename11](https://forum.freecodecamp.org/u/codename11)\
**Post date:** [May 5, 2018, 8:12pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/11 "2018-05-05T20:12:02Z")

</div>

Thanks anyway. I found appropriate solution.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [May 5, 2018, 8:50pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/12 "2018-05-05T20:50:09Z")

</div>

I hope you try to understand those concepts I was pushing. There are ways around those, but those are pretty fundamental concepts to JS and web dev.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:22pm UTC](https://forum.freecodecamp.org/t/determine-if-twitch-stream-is-offline/189395/13 "2021-01-17T20:22:11Z")

</div>

Closed on January 17, 2021, 8:22pm UTC


