Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeout #144

Merged
merged 1 commit into from
Dec 19, 2024
Merged

Fix timeout #144

merged 1 commit into from
Dec 19, 2024

Conversation

zhiyuanliang-ms
Copy link
Contributor

@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.

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.

2 participants