[英]How to call multiple mutations at the same time?
我有一個id數組,我創建了一個變異,允許我只使用1個id刪除一個項目。
有沒有辦法使用Relay.Store.commitUpdate
或this.props.relay.commitUpdate
多次調用此突變?
我想你可以在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);
},
});
}
並將您的突變作為承諾數組提交並使用Promise.all捕獲結果(但請記住其快速失敗行為)。 它可能是這樣的:
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.