0

I have this var that i need to make global so i can use it in another area, i can't find away around, i tried making it global but didn't work. can i get help pls ? :)

fetch('http:google.com').then(res => res.json()).then((out) => {
var x = _.filter( out.features, ['status','online']);
console.log('Output: ', x);
}).catch(err => console.error(err));

i need to make X global var so i can use in different place in my website. for example in the next place i need just to call it var x and get the data.

MrBlue
  • 1

1 Answers1

0

There are a few ways you could do this:

Declare it on the window so you can access it elsewhere:

window.x = yourResponseHere;

Declare the variable in the global scope and then set it within your fetch:

var x = ""
fetch('http:google.com').then(res => res.json()).then((out) => {
x = _.filter( out.features, ['status','online']);
console.log('Output: ', x);
}).catch(err => console.error(err));
Luke Glazebrook
  • 580
  • 2
  • 14
  • You really should have a look at the _possible duplicate_ in the comments and how [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) work :) – Andreas Oct 08 '17 at 09:32