Attempt tries to resolve promises.
- Can try to get valuable data from server over unstable mobile networks.
- Can handle some unexpected network lags with 0 code or timeouts.
For example, user of your service have some critical data and you want to deliver it to your server. User browsing under unstable and slow cellular network. You may give up on first fail or try to fix this problem, using callback hell or use this solution :)
Assume you have rule which defines behaviour of repeating requests:
- each next repeat should be performed in
N * 500ms
, where N is attempt number error.status >= 500
- no reason to repeat, your service is totally downerror.status === 400
- no reason to repeat, something wrong with input dataerror.status === 0
(aka abort) - repeat- if number of repeats is more than 5 - ask user continue to repeat or not
- if number of repeats is more than 10 - do not repeat
And instead of calling promise directly
$;
wrap it with
{ return $;} { // no reason to repeat if err && errstatus === 400 || errstatus >= 500 return false; // if number of repeats is more than 10 - do not repeat if attemptNo > 10 return false; // if number of repeats is more than 5 - ask user what repeat or not if attemptNo > 5 return ; // each next repeat should be performed in N * 500ms return attemptNo * 500;};
Examples
Configure attempt
By default Attempt
tries to use global Promises, but you can specify your own promises.
attempt;
Or use old-style promises approach:
attempt;
Synchronous example
; { return ;} { console;} { console;}
Async example
;
Promise example
;
Attempt number in promise Factory function
;