score:1
Accepted answer
i'd suggest using promises.
readdata: function(){
return new promise(function(resolve) {
var readfromcpdev1 = firebase.database().ref('environments/' + 'cp-dev1');
var envusersarray = [];
readfromcpdev1.on('value', function(snapshot) {
envusersarray.push(snapshot.val())
resolve(envusersarray);
});
});
},
anotherfunc: function(){
this.readdata().then(function(arr) {
//do stuff here
});
}
score:0
you may return a promise that will resolve with value that is retrieved asynchronously:
readdata: function() {
var readfromcpdev1 = firebase.database().ref('environments/' + 'cp-dev1');
var envusersarray = [];
return new promise(function(success) {
readfromcpdev1.on('value', function(snapshot) {
envusersarray.push(snapshot.val())
success(envusersarray);
}
});
},
anotherfunc: function(){
this.readdata().then(function(result) {
console.log(result);
});
}
Source: stackoverflow.com
Related Query
- Passing an array to another function in ReactJS
- Reactjs : Passing array as prop to child function component. Modifying the array in child is causing state mutation exception
- Passing string parameters into function from one component to another in reactjs
- i am passing parameter from one function to another function in reactJs but result is coming null
- Passing a function with parameters through props on reactjs
- React useEffect - passing a function in the dependency array
- Reactjs - passing state value from one component to another
- Passing array value from one class to another
- How to call another component from onClick function in ReactJS
- Using a function in the render state in ReactJS to append a div to another
- onClick event not passing a function when placed in a forEach loop in ReactJS
- reactjs passing array of components
- Calling a function from another in ReactJs
- reactjs how to filter / check if a any object in an array is present in another array of objects?
- Passing data from one function to another in React
- Passing a function to Child Component through a Prop using ReactJS
- ReactJS passing object instead of function inside onClick
- Prop undefined when passing to a function in another .js file
- ReactJS - Component not passing this.state.object properly to another component
- Get object array from axios and using return value call another axios function and append the output to first result
- ReactJS error when passing a function as a props
- Why is my conditional function of adding an item from an array to another one not working?
- Need to Trigger individual Element in an array of elements written through map function reactjs
- Passing multiple parameters to function in reactjs
- pass function argument to another component reactjs
- Passing $& in replace()-function to another function
- How can I make a function Async in reactjs which calls another two functions from redux actions
- Call a method from another Component after the Async function reactjs
- Passing props to function imported from another file
- Passing a function to child component in reactjs
More Query from same tag
- Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined react-dom.production.min.js:760
- How to make only one service layer for all request in react js?
- Separate a function from a React component (improve modularity)
- Next.js: How to get applied styles from element?
- Remove Checkbox Outline in Material-UI
- Is there a way to change the values of multiple items in a functional component object? React
- Can I put ajax in React component constructor?
- React change login link to logout after login
- Unhandled Rejection (TypeError): api.getUser is not a function
- Use React Toast Notification in class component
- React : Returning SVG contents as variable?
- Google APi + Passport + React : Authentication flow
- Could not find store in either context or props
- In react - updating an array from child component to a parent component
- How can i persist only a subset of state from React Redux?
- React: setState value not working when refreshing the web
- ReactJS: Maximum update depth exceeded when fetching data from local-storage token
- Styling SVG as a React Component
- (Isomorphic) Back-end rendering of routes and ajax-based react components
- React Child Component Not Rendering
- JSX extra white space when using Split Map
- Dropdown menu - disappear after clicking on item
- Google maps marker Showing wrong Position?
- Adjacent component interaction in Thermite
- re-render component in react to get default props again
- Import css File in asp.net core2 with react redux
- Create React App - console lint warnings don't match eslintrc
- Trying to figure out how to use socket.io the correct way in a useEffect that is using an axios get request to fetch messages
- Pass components as props to another component
- I want to pass my fetch results to setState. When I console.log(setUserApartment) it doesn't show the data from foundApartment. How can I setState?