score:1
Accepted answer
you aren't waiting for all of your promises to resolve before logging the array, the main issue is that you don't return the promise from your map function, you could do:
const getdata = async () => {
try {
let array = [];
const firstdata = await axios.get(`https://swapi.dev/api/films/1/`)
await promise.all(
firstdata.data["characters"].map(url=>{
return axios.get(url).then(character=>{
array.push(character.data.name);
})
})
).then(result=>{
console.log("array:",array);
json.stringify(array);
})
} catch (err ) {
// errors
console.log(err,"connection error")
}
}
Source: stackoverflow.com
Related Query
- React Asynchronous nested data fetching
- fetching data with react hook returns undefined on nested obj properties
- Displaying Nested Objects in React after fetching the data from an api?
- Set the data in React Context from asynchronous API call
- Recursively render a deep nested data in React
- Render recursively a nested data in React
- How to perform a server side data fetching with React Hooks
- React setState doesn't re-render after fetching data
- Fetching large amount of data with loop or recursion with progress percentage in React / React native
- REACT - Set initial data in Formik Form after fetching from API
- React Redux fetching data from backend approach
- React JS printing data inside a nested map function in JSX
- show loading when fetching data react
- Fetching data from store if exists or call API otherwise in React
- Proper Way in Fetching Initial Data in React Boilerplate Container
- [React]different ways of fetching asynchronous data into component
- Implement nested async route fetching in React
- React best-practices -- pure or non-pure components? fetching data in componentWillMount?
- Fetching data from mongo via express, build object, and send to React
- How to prevent React Hook's useEffect from fetching data multiple times
- How to do Server Side Rendering in React With React Loadable and Fetching Data for Components (Like Next.js)?
- Passing data up through nested Components in React
- React not fetching data from the updated env file
- React - Accessing nested objects in returned data
- react useEffect is running and fetching data none stop
- React - Nested maps to retrieve json data
- No correct data in fetching in react hook
- How to use Promise.all properly fetching data for React app
- Problem fetching data from inner join table with axios in react
- Immutable update pattern in react for nested data
More Query from same tag
- React: Function Map doesn't work in my render()
- Component loses data from states on reload
- Changing Views in ReactJS
- Customize Material UI ToolTip
- Prevent re-render using React.memo and React.useCallback
- How to write a unit test to check if modal shows up after clicking some link
- Material-UI include only certain component
- React Native - CSS :last-child without a border?
- Manipulation the zoom property in CSS3
- I cannot understand what the error is and why e.currentTarget.value is null
- How to build a nested object on handleChange with React?
- Im trying to generate bar chart.js
- alternating between ascending and descending Sorting on Click reactjs
- React can't make if statement with state
- I am not able to initialize create-react-app for some strange reason
- What is the purpose of using JSON.stringify()?
- React Reflux Stores: Difference between calling trigger() with parameters vs. without parameters
- How to recursively export all from folders in node js project?
- How to toggle using redux in react for a nested link component within a mobile menu
- Displaying React component on same line as other elements
- react-router: How to set default child route and modify URL accordingly
- React - Babel not transpiling files
- How to fill users variable with data from GET request in React
- React-day-picker test with enzyme & jest
- Chart JS in React doesn't fetch data
- setState updating but values not being retained
- How to use React Moment Use fromNow in element title?
- React hook form two inputs with same register key
- Using webpacks raw-loader to read markdown files results in contents of index.html
- Specific geojson URL is not working within my react app using deck.gl and nebula.gl