Tuesday, 6 September 2016

es6 promise - Javascript - access variable in then outside it's scope

I have the following Javascript and I am trying to access the content data out of the service scope, what's the best way to access that data?



Service
.getContent()
.then((content = {}) => {//access this content out of 'Service' scope})
.catch((error = {}) => {console.log('errror', error)})



I tried the following:



let data = null;
Service
.getContent()
.then((content = {}) => {data = content})
.catch((error = {}) => {console.log('errror', error)})
console.log(data);



But I get the error that data is undefined. How can I get the contents of content to data

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...