FetchJS addSubscriber possible problem

Hi,

I was following this guide: https://www.fusetools.com/developers/guides/fusejs/fetchjson and it kept crashing. So I changed it to this:

    data.addSubscriber(function() {
      // Assuming description is a member of the returned JSON
      // object from the url

      debug_log("got some data " + data);
      //debug_log("Got some data: " + data.value.arne);
    });

And it gave the following output: Output: got some data (observable) Output: got some data (observable) {“arne”:“arnemann”}

So it will be called to times. One time with undefined in data or data.value. Is this the correct behaviour?

Yes, this is intended behavior.

When you subscribe, it will immediately call your function with the current data, which is undefined until it actually gets the data back (which may be never, if the request fails).

FetchJson is meant for reactive programming, not for running imparative code in response to a HTTP request.

You have a few options here:

  • Do a check for if (data === undefined) return;
  • Use XMLHttpRequest instead of FetchJson
  • Use fetch().then(.. instead of FetchJson. fetch() is available starting the next release (after v0.5.3709)

Thanks.

You might want to update the example then, as it crashes for me.