扫码一下
查看教程更方便
waitforall(dependencies) 一个允许我们并发计算多个异步依赖项的并发 helper 方法。
依赖项可以作为元组数组提供,也可以作为对象中的命名依赖项提供。
function waitforall(dependencies: array>):
recoilvaluereadonly
function waitforall(dependencies: {[string]: recoilvalue<>}):
recoilvaluereadonly
因为此并发 helper 是作为一个 selector 提供的,所以它可以作为 react 组件中的 recoil 钩子函数使用,也可以作为 recoil selector 中的依赖项使用,或者任何使用 recoil 状态的地方。
function friendsinfo() {
const [frienda, friendb] = userecoilvalue(
waitforall([friendastate, friendbstate])
);
return (
friend a name: {frienda.name}
friend b name: {friendb.name}
);
}
const friendsinfoquery = selector({
key: 'friendsinfoquery',
get: ({get}) => {
const {friendlist} = get(currentuserinfoquery);
const friends = get(waitforall(
friendlist.map(friendid => userinfoquery(friendid))
));
return friends;
},
});
const customerinfoquery = selectorfamily({
key: 'customerinfoquery',
get: id => ({get}) => {
const {info, invoices, payments} = get(waitforall({
info: customerinfoquery(id),
invoices: invoicesquery(id),
payments: paymentsquery(id),
}));
return {
name: info.name,
transactions: [
...invoices,
...payments,
],
};
},
});