When Good AJAX Goes Bad

I’ve prepared a slightly different observable for you in errors.js. This new observable still has all the requests, but they hit a different endpoint on the node server. This new endpoint is programmed to give up and start spitting errors after several dozen requests. Using your knowledge from earlier in the chapter, pass in a function to handle the case where some requests fail to complete.

 loadingRequestsBad$
 .pipe(
  scan((prev) => prev + (100 / arrayOfRequests.length), 0)
 )
 .subscribe(percentDone => {
  progressBar.style.width = percentDone + ​'%'​;
  progressBar.innerText = Math.round(percentDone) + ​'%'​;
 }, err => {
  console.log(err);
  msgArea.innerText = ​'Something went wrong, please try again'​;
  msgArea.style.display = ​'block'​;
 });

Aha! Now when the server falls over, it’s gracefully handled—the user is informed that something’s gone wrong and has some basic remediation instructions. You’ve built an app that can survive even the harshest of conditions without skipping a beat. Users may vent on Twitter, but at least they’ll be venting about the right thing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.34.25