diff --git a/src/api-fetch-setup.js b/src/api-fetch-setup.js index be68b3d8f0..5bf24ba70f 100644 --- a/src/api-fetch-setup.js +++ b/src/api-fetch-setup.js @@ -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 }` ); } @@ -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 ) ); } ); };