Return a Promise when calling a function in the table: Try and return a Promise function call
P粉340264283
2023-09-05 14:36:01
<p><pre class="brush:php;toolbar:false;">let data = [223, 34, 456, 56, 67];
function getDataFromApi(paramfromTableCell){
let postData = {data : paramfromTableCell}
let result = apiResponse(url, 'post', postData).catch((err => console.log(err)))
return result;
}
data.map((value)=>{
return(
<th>{getDataFromApi(value)}</th>
)
})</pre>
<p>Calling a function in a table cell, but it returns a Promise. When calling the function, it takes one parameter and returns the name based on the number, but it returns a Promise. Is there any way to solve this problem? </p>
You must
await
this promise to get the result. Otherwise you will just get this promise. So addasync
in your map function and then useawait
: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):