I'm currently working on some code that calls an async function and has a Promise returned. The call history is not important, but I'll include all relevant code.
function Bookshelf(props) {
const { genre } = props;
const books = [];
const arrayOfPromises = [];
arrayOfPromises.push(getBooksUnderGenre(genre)) // this is the async function
Promise.all(arrayOfPromises)
.then(results => {
console.log(results);
const result = results[0];
books.push(result);
});
books.map((book, i) => {
return (
<Text> {book.name} </Text>
)
});
};
My problem here, is when I console.log(results)
, I clearly see all of the results I wish to have in my terminal, yet const books = []
is empty so I'm essentially mapping over nothing. Is there a way I can do this without making Bookshelf
asynchronous itself? If not, what would I have to do?
For reference, this is what I get when I log results
[
[
{
genre: "fantasy",
id: "19",
type: "book",
name: "Star Night",
},
{
genre: "fantasy",
id: "20",
type: "book",
name: "A Moon Far Away",
},
{
genre: "fantasy",
id: "21",
type: "book",
name: "Green Lands",
},
],
];
(Just a note, I typed this all so if there are any typos / syntax errors, it's not like that in my code, so that wouldn't be a problem).