Skip to content

Use Promise.allSettled instead of Promise.all for partial failure handling #81

@umairkarel

Description

@umairkarel

Promise.allSettled should be used

Promise.all(requests)

Description

The current implementation uses Promise.all(requests) but later assumes that all request results will always be available, even if some requests fail.

However, Promise.all() rejects immediately when any promise rejects, which means:

  • The .then(results => { ... }) block will not execute if any request fails or times out
  • Per-request logging inside results.forEach(...) will be skipped
  • Failures are handled only by .catch(), losing visibility into successful responses

This behavior limits executing, evaluating and logging each individual requests and responses.

Solution

Replace Promise.all() with Promise.allSettled() so that all requests are awaited, regardless of individual failures.

This allows:

  • Logging of both successful and failed requests
  • Accurate detection of partial failures
  • Allows all requests to be executed even if one or more requests fail in between.

Proposed Implementation

Promise.allSettled(requests)
  .then((results) => {
    let someRequestFailed = false;

    results.forEach((result) => {
      if (result.status === 'fulfilled') {
        const res = result.value;

        log({
          Name: 'Facebook',
          Type: 'Response',
          TraceId: traceId,
          EventName: mappedEventData.event_name,
          ResponseStatusCode: res.statusCode,
          ResponseHeaders: res.headers,
          ResponseBody: res.body
        });

        if (res.statusCode < 200 || res.statusCode >= 300) {
          someRequestFailed = true;
        }
      } else {
        someRequestFailed = true;

        log({
          Name: 'Facebook',
          Type: 'Response',
          TraceId: traceId,
          EventName: mappedEventData.event_name,
          Message: 'Request failed or timed out.',
          Reason: JSON.stringify(result.reason)
        });
      }
    });

    if (!data.useOptimisticScenario) {
      if (someRequestFailed) data.gtmOnFailure();
      else data.gtmOnSuccess();
    }
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions