Skip to content

Conversation

@zhiyuanliang-ms
Copy link
Member

@zhiyuanliang-ms zhiyuanliang-ms commented Dec 19, 2024

Why this PR?

Promise.race will not cancel other pending promises. The setTimeout will keep the node process running.

This bug caused this call: const settings = await load(connectionString); will always let the node process run for 30 seconds.

An example to illustrate the issue:

async function foo() {
  await new Promise(resolve => setTimeout(resolve, 1000));
  return "done";
}

async function main() {
    let timeoutId;
    const ms = 5000;
    const timeoutPromise = new Promise((_, reject) => {
        timeoutId = setTimeout(() => {
          reject(new Error(`Operation timed out after ${ms}ms`));
        }, ms);
      });

    try {
        let result = await Promise.race([foo(), timeoutPromise]);   
    }  catch (error) {
        throw new Error(`Failed to build fallback clients, ${error.message}`);
    } 
    ////// Solution
    // finally {
    //     clearTimeout(timeoutId);
    // }
    //////
}

main();

The node process will run for 5 secs instead of ending after foo is done.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

@zhiyuanliang-ms zhiyuanliang-ms merged commit 8600654 into preview Dec 19, 2024
4 checks passed
@zhiyuanliang-ms zhiyuanliang-ms deleted the zhiyuanliang/fix-timeout branch December 19, 2024 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants