JSFiddle - React, Tailwind, and code Playground

by Kikketer

JavaScript

/* for(var i=0; i<max; i++) {
    p = p.catch(attempt).then(test).catch(rejectDelay);
    // Don't be tempted to simplify this to `p.catch(attempt).then(test, rejectDelay)`. Test failures would not be caught.
}
p = p.then(processResult).catch(errorHandler); */

// Put into the client-core:
const whenOnline = async (times, time) => {
console.log('Called whenOnline')

	const testNetwork = async fail => {
	  // The client-core call
    console.log('Testing! ', fail)
		// mFetch.
    await new Promise((resolve, reject) => {
    	setTimeout(() => {
	      console.log('Timout started ', fail)
	      if(fail) {
    	  	reject(new Error('you are offline'))
	      } else {
  	    	resolve()
        }
      }, 300)
    })
  }
  
  try {
	  time++
	  await testNetwork(time !== times)
  } catch(e) {
	  console.log('err ', e)
    // Delay the next try
    await new Promise(resolve => setTimeout(resolve, 2000))
		return whenOnline(times, time)
  }
}

// Clients network helper:
const onOnline = async callback => {
	// make the app have a warning "you are offline"
	await whenOnline(4, 0)
  callback()
}


/* window.addEventListner('online', onOnline) */
onOnline(() => {
  console.log('Start the socket')
  console.log('Reload content')})