• Using fetch with async/await

    • try {
        const res = await fetch('/ping');
        if(!res.ok) {
          throw new Error('Bad response', {
            cause: {res}
          })
        }
        const pong = res.text();
      } catch(err) {
        switch(err.cause.res?.status){
          case 500: console.log('Internal server error');
            break;
          case 401: console.log('Unauthorised')
            break;
            //... keep going to handle other cases
        }
      }
  • use Promise.allSettled() instead of Promise.all()

    • promises
    • Using Promise.all() has potential dangers because if one of the promises fail, the rest of the promises run but we never get back the response. The resolved promises are lost in the void.
    • Use Promise.allSettled() instead because what this returns is an array of results with status and resolved value of each promise.
    • Using Promise.allSettled() makes us do a bit of extra work to process the resolve and rejects separately but its for the good.
    • {{embed ((64958d37-5aba-4a90-a9b2-40dd81e93984))}}