Skip to content

Timeout method in promises.ts has nested promises and doesn't abort promise after timeout #63

@superbrobenji

Description

@superbrobenji

currently the timeout function looks as follows:

function timeout<T>(promise: Promise<T>, time: number = 8000) {
  return new Promise((resolve, reject) => {
    const timeout_id = setTimeout(
      () => reject(new Error(TimeoutErrors.TIMEOUT_ERROR_MESSAGE)),
      time
    );

    promise
      .then(response => {
        clearTimeout(timeout_id);
        resolve(response);
      })
      .catch(err => {
        clearTimeout(timeout_id);
        reject(err);
      });
  });
}

at the moment it, the passed promise is nested within a new promse that's returned. it will return on settlement of the passed promise or it will reject on the settlement of the settimeout. this logic does not abort the passed promise. If it's an api request for instance, the request still takes place, we just no longer wait for the response.

A solution would be to instead create an abortPromise with an abortController, and Promise.race it against the passed promise. this way we no longer have nested promises, and the promise gets aborted entirely, not just ignored

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions