P粉9707363842023-09-06 12:12:35
You must await
this promise to get the result. Otherwise you will just get this promise. So add async
in your map function and then use await
:
data.map(async (value)=>{ return(<th>{await getDataFromApi(value)}</th> )
P粉8322127762023-09-06 00:30:22
Looks like you are using React. You need to save your response into React's state.
Here is a sample code, it should look like this (untested):
let data = [223, 34, 456, 56, 67]; const [responses, setResponses] = useState([]); useEffect(() => { const getAllResponses = () => Promise.all( data.map(val => getDataFromApi(val)) ); getAllResponses().then(responses => setResponses(responses)); }, [data]) function getDataFromApi(paramfromTableCell){ let postData = {data : paramfromTableCell} return apiResponse(url, 'post', postData).catch((err => console.log(err))) } responses.map((value)=>{ return( <th>{value}</th> ) })