我看过许多文章和现场示例,介绍了通过添加ACTION_FAIL/ACTION_SUCCESS
有时添加ACTION_ATTEMPT/ACTION_START
处理redux-thunk中的错误。很好,虽然我所看到的所有情况似乎都向用户提供了一个简单的通知,例如“糟糕,这出了错。请重试!”早点回家。
我要寻找的情况是进行无缝的dispatch-error-retry-success
过渡,或者至少比向每个thunk添加额外的动作分派更好或更可维护的解决方案。
一个例子是令牌到期。 如果令牌即将到期或由于令牌失效而失败(并且注销不是一个好选择),则刷新令牌并重试请求可能是一种解决方案,而不会导致用户刮擦他的头。
我很想听听:
答案 0 :(得分:1)
如果您可以自动处理故障(例如您提到的刷新令牌示例),则根本不需要分派额外的操作。您只需在重击中重试即可,如果成功恢复,则可以调度成功操作。
to_delete = []
for person in Person.objects.all():
# all people sharing a last name with person
matches = Person.objects.filter(last_name=person.last_name)
# a list with the first names
first_names = matches.values_list('first_name', flat=True)
# a list with the last names
last_names = matches.values_list('last_name', flat=True)
# Join them with comma as a separator e.g 'Alan, Inna'
joined_fnames = ', '.join(first_names)
joined_lnames = ', '.join(last_names)
# set the new joined strings as persons new values
person.first_name = joined_fnames
person.last_name = joined_lnames
# get the other record ids that have already been joined into person and add to to_delete
ids = matches.exclude(id=person.id).values_list('id', flat=True)
to_delete += ids
person.save()
# finally delete all records in to_delete
Person.objects.filter(id__in=to_delete).delete()
调度额外的“重试”操作的唯一原因是,如果您想在用户界面中传达临时故障。