Ali*_*eed 7 javascript yield ecmascript-6 redux-saga
是否有使用终极版,传奇的任何优势yield all([])了ES6的内置yield []?
为了并行运行多个操作,redux-saga建议:
const result = yield all([
call(fetchData),
put(FETCH_DATA_STARTED),
]);
Run Code Online (Sandbox Code Playgroud)
但是没有all()方法可以实现同样的目的:
const result = yield [
call(fetchData),
put(FETCH_DATA_STARTED),
];
Run Code Online (Sandbox Code Playgroud)
哪一个更好?为什么?
没有功能差异,因为MateuszBurzyński(redux-saga维护者)在这里解释:
在引擎盖下,它们都是相同的,但
yield [...effects]会导致弃用警告,并通知您all.引入它是为了使并行行为显式化,并很好地镜像Promise.all
它最好使用,all()因为它告诉读者我们在这里产生了超过1个效果,但是如果没有它,yield的各种用途仍然有用:
产生具有多种效果的物体
const { company, profile } = yield {
company: select(getCompany),
profile: select(getUserProfile, userId),
};
Run Code Online (Sandbox Code Playgroud)
产生一个数组文字
yield [
put(userRequestSucceeded(userId)),
put(userReceived(response.data)),
];
Run Code Online (Sandbox Code Playgroud)
使用map生成一个数组
yield userIds.map(userId => call(fetchUserDetails, userId));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5631 次 |
| 最近记录: |