1- import asyncify from 'async/asyncify' ;
2- import retry from 'async/retry' ;
3- import assign from 'lodash/assign' ;
1+ import retryit from 'retryit' ;
42
53import { loadFailure , loadSuccess } from './actions' ;
64import { isAction } from './utils' ;
@@ -12,11 +10,11 @@ export default class Task {
1210 throw new Error ( 'action must be a plain object' ) ;
1311 }
1412
15- this . context = assign ( { } , context , {
13+ this . context = Object . assign ( { } , context , {
1614 action : monitoredAction ,
1715 } ) ;
1816
19- this . params = assign ( { } , {
17+ this . params = Object . assign ( { } , {
2018 success ( { action } ) {
2119 throw new Error ( 'success() is not implemented' , action . type ) ;
2220 } ,
@@ -35,8 +33,8 @@ export default class Task {
3533 } , params ) ;
3634 }
3735
38- execute ( options = { } , callback ) {
39- const opts = assign ( { } , DEFAULT_OPTIONS , options ) ;
36+ execute ( options = { } ) {
37+ const opts = Object . assign ( { } , DEFAULT_OPTIONS , options ) ;
4038
4139 const context = this . context ;
4240 const dispatch = context . dispatch ;
@@ -51,37 +49,33 @@ export default class Task {
5149 const disableInternalAction = options . disableInternalAction ;
5250
5351 if ( ! shouldFetch ( context ) ) {
54- callback ( null , null ) ; // load nothing
5552 if ( ! disableInternalAction ) {
5653 const successAction = loadSuccess ( context . action ) ;
5754 dispatch ( successAction ) ;
5855 }
59- return ;
56+ return Promise . resolve ( ) ;
6057 }
6158
6259 dispatch ( loading ( context ) ) ;
6360
6461 // Retry
65- const asyncFetch = asyncify ( fetch ) ;
66- retry ( {
62+ return retryit ( {
6763 times : opts . retryTimes ,
6864 interval : opts . retryWait ,
69- } , ( retryCb ) => {
70- asyncFetch ( context , retryCb ) ;
71- } , ( err , result ) => {
72- if ( err ) {
65+ } , ( ) => Promise . resolve ( fetch ( context ) ) )
66+ . then ( ( result ) => {
67+ const successAction = success ( context , result ) ;
68+ if ( ! disableInternalAction ) {
69+ dispatch ( loadSuccess ( context . action , result ) ) ;
70+ }
71+ return dispatch ( successAction ) ;
72+ } )
73+ . catch ( ( err ) => {
7374 const errorAction = error ( context , err ) ;
7475 if ( ! disableInternalAction ) {
7576 dispatch ( loadFailure ( context . action , err ) ) ;
7677 }
77- callback ( null , dispatch ( errorAction ) ) ;
78- return ;
79- }
80- const successAction = success ( context , result ) ;
81- callback ( null , dispatch ( successAction ) ) ;
82- if ( ! disableInternalAction ) {
83- dispatch ( loadSuccess ( context . action , result ) ) ;
84- }
85- } ) ;
78+ return dispatch ( errorAction ) ;
79+ } ) ;
8680 }
8781}
0 commit comments