Теперь я создаю приложение для отслеживания книг с использованием реагирования и редукции, мне нужно асинхронно получать данные, и теперь у меня есть 3 версии кода, две из которых возвращают ожидаемый результат, но не третий
Первый:
import { FETCH_BOOKS } from '../actions/Types.js';
import * as BooksAPI from '../BooksAPI'
export const bookReducer = (state = [], action) => {
switch(action.type) {
case FETCH_BOOKS:
console.log (BooksAPI.getAll().then(books => books))
default:
return state;
}
}
//Promise {<pending>}
//this returns a promise in a pending state that is resolved to array of objects(expected)
Второй:
import { FETCH_BOOKS } from '../actions/Types.js';
import * as BooksAPI from '../BooksAPI'
export const bookReducer = (state = [], action) => {
switch(action.type) {
case FETCH_BOOKS:
console.log ([BooksAPI.getAll().then(books => books)])
default:
return state;
}
}
//[promise]
//this returns an array of a promise that is resolved to array of another array of books objects(expected)
Третий:
import { FETCH_BOOKS } from '../actions/Types.js';
import * as BooksAPI from '../BooksAPI'
export const bookReducer = (state = [], action) => {
switch(action.type) {
case FETCH_BOOKS:
console.log ([...BooksAPI.getAll().then(books => books)])
default:
return state;
}
}
//[]
//this returns an empty array, no promise
//expected spreading items of the returned array of objects not empty array!!
Так что здесь не так ??