Skip to content

Ensure devicePool.release is called for both providers even if one rejects #4339

@greggman

Description

@greggman

Just want to double check this code is doing what's expected.

// Ensure devicePool.release is called for both providers even if one rejects.

    // Ensure devicePool.release is called for both providers even if one rejects.
    await Promise.all([
      this.provider?.then(x => devicePool.release(x)),
      this.mismatchedProvider?.then(x => mismatchedDevicePool.release(x)),
    ]);

It will call devicePool.release on both. It will not wait before proceeding as Promise.all continues as soon as one promise rejects.

Is that fine that it doesn't wait? If we want it to wait then I think you'd need something like

    // Ensure devicePool.release is called for both providers even if one rejects.
    const results = await Promise.allSettled([
      this.provider?.then(x => devicePool.release(x)),
      this.mismatchedProvider?.then(x => mismatchedDevicePool.release(x)),
    ]);

    const rejected = results.find(result => result.status === 'rejected');
    if (rejected) {
      throw rejected.reason;
    }

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