我已将代码更改为此,但仍无法正常工作... 这给了我一个诺言,但是数据迟到了(异步)
import { getItems } from '../services/itemService';
function delay() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(42);
}, 1000);
});
}
const getAllItems = async () => {
try {
var { data } = await getItems();
await delay();
return data.map(obj => ({ ...obj, value: 0 }));
} catch (error) {
return null;
}
};
const states = getAllItems();
const defaultState = states.then(value => {
console.log(value);
return value;
});
console.log(defaultState);
export default defaultState;