Skip to content

Commit

Permalink
Adding retry feature to mobile apiFetch handler
Browse files Browse the repository at this point in the history
  • Loading branch information
etoledom committed Dec 16, 2019
1 parent 06b924d commit 5b1872d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/api-fetch-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { fetchRequest } from 'react-native-gutenberg-bridge';
*/
import apiFetch from '@wordpress/api-fetch';

const fetchHandler = ( { path } ) => {
const setTimeoutPromise = ( delay ) => new Promise( ( resolve ) => setTimeout( resolve, delay ) );

const fetchHandler = ( { path }, retries = 20 ) => {
if ( ! isPathSupported( path ) ) {
return Promise.reject( `Unsupported path: ${ path }` );
}
Expand All @@ -27,7 +29,12 @@ const fetchHandler = ( { path } ) => {
return responsePromise.then( parseResponse ).catch( ( error ) => {
// eslint-disable-next-line no-console
console.warn( 'Network Error: ', error );
return Promise.resolve( error );
if ( error.code >= 400 && error.code < 600 ) {
return Promise.reject( error );
} else if ( retries === 0 ) {
return Promise.reject( error );
}
return setTimeoutPromise( 2000 ).then( () => fetchHandler( { path }, retries - 1 ) );
} );
};

Expand Down

0 comments on commit 5b1872d

Please sign in to comment.