[英]How to call multiple mutations at the same time?
I have an array of ids, and I created a mutation that allow me to delete an item using only 1 id. 我有一个id数组,我创建了一个变异,允许我只使用1个id删除一个项目。
Is there any way to call this mutation multiple times using Relay.Store.commitUpdate
or this.props.relay.commitUpdate
? 有没有办法使用
Relay.Store.commitUpdate
或this.props.relay.commitUpdate
多次调用此突变?
I think you can wrap each Relay.Store.commitUpdate in Promise: 我想你可以在Promise中包装每个Relay.Store.commitUpdate:
commitMutationPromise = (Mutation, data) =>
new Promise((resolve, reject) => {
Relay.Store.commitUpdate(new Mutation(data), {
onSuccess: (transaction) => {
resolve(transaction);
},
onFailure: (transaction) => {
reject(transaction);
},
});
}
And commit your mutations as array of promises and catch result with Promise.all(but keep in mind its fail-fast behaviour). 并将您的突变作为承诺数组提交并使用Promise.all捕获结果(但请记住其快速失败行为)。 It could be something like this:
它可能是这样的:
handleDelete = (deleteIds) => {
const deletePromisesArray = [];
deleteIds.forEach(id => {
deletePromisesArray.push(
this.commitMutationPromise(DeleteMutation, { id })
);
});
Promise.all(deletePromisesArray).then(values => {
this.onSuccessDelete(result);
}, error => {
this.onFailDelete(error);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.