曾被要求重构。该代码正在运行,但不确定最终会破坏什么:
export function* fetchWidgetDataIfRequired(action) {
yield call(taggedDelay, 102);
const { dashboardId, dashboardType } = action.payload;
const dashboard = yield select(makeSelectDashboard({ dashboardId, dashboardType }));
const widgetData = yield select(makeSelectWidgets());
const dashboardWidgets = dashboard.getIn(['config', 'widgets']);
// get all widgets that have userWidgetId but no widgetLayoutData
// NOTE: if you do not do valueSeq(), it will still work but it will throw a warning about
// > [...effects] has been deprecated in favor of all([...effects]), please update your code
const fetchWidgetLayoutDataActions = dashboardWidgets.filter((widget) => {
'';
const userWidgetId = widget.get('userWidgetId');
return userWidgetId != null
&& widgetData.getIn([userWidgetId, 'widgetLayoutData']) == null;
}).map((widget) => yield spawn(fetchWidgetLayoutData, { dashboardType, dashboardId, widget })).valueSeq().toArray();
// console.error('fetchdata', {
// fetchWidgetLayoutDataActions,
// });
yield all(fetchWidgetLayoutDataActions);
// get all widgets that have userWidgetId but no widgetData
const fetchWidgetDataActions = dashboardWidgets.filter((widget) => {
const userWidgetId = widget.get('userWidgetId');
return userWidgetId != null
&& widgetData.getIn([userWidgetId, 'widgetData']) == null;
}).map((widget) => {
const userWidgetId = widget.get('userWidgetId');
const delay = widget.get('y', 0) * 250 + 6;
// const delay = 100;
return spawn(fetchWidgetData, { userWidgetId, delay });
}).valueSeq().toArray();
yield all(fetchWidgetDataActions);
}
我被抛出此错误:
A 'yield' expression is only allowed in a generator body.ts(1163)
我很想知道如何更好地对此代码进行编码,以避免出现此错误。
关注功能:
.map((widget) => { // NOT A GENERATOR
const userWidgetId = widget.get('userWidgetId');
const delay = widget.get('y', 0) * 250 + 6;
return yield spawn(fetchWidgetData, { userWidgetId, delay }); // TRIED TO USE YIELD
})
错误是因为函数不是生成器,但是您尝试使用yield。
代码是错误,因此TypeScript抱怨。我们只能在没有完整上下文的情况下推测正确的代码,但很可能以下是正确的:
.map((widget) => {
const userWidgetId = widget.get('userWidgetId');
const delay = widget.get('y', 0) * 250 + 6;
return spawn(fetchWidgetData, { userWidgetId, delay }); // NO YIELD
})