DEV Community

Cover image for 403 Errors, Server too busy?  Here's a solution.
John Peters
John Peters

Posted on • Updated on

403 Errors, Server too busy? Here's a solution.

Our backend didn't have an endpoint to save an array of persons. Forkjoin didn't work either as it fired off post requests too rapidly.

We tried an interval first without the awaiter:

let sub =
  interval(2000).subscribe(async (k) => 
  let person = this.persons[k];
  if (person) {
    await this.savePerson(persons);
  }else{ 
     sub.unsubscribe(); 
  }
Enter fullscreen mode Exit fullscreen mode

That didn't work because all of the posts were fired at the same time as if they were waiting for the post-angular-rendering cycle. We know from past experience that http requests don't happen until the end. After putting in the await coupled with this code we were in luck.

async savePerson(person) {
 return new Promise((resolve, reject) => {
   this.rs.postPerson(person).subscribe(
   (result) => {
     resolve(result);
   },
   (error) => {
      reject("");
   });
  }
 }
Enter fullscreen mode Exit fullscreen mode

Now we have full control over how fast our posts are fired off.

Discussion (0)