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

Script completes without waiting for timeouts it scheduled #60

Open
thorn0 opened this issue Mar 7, 2025 · 1 comment
Open

Script completes without waiting for timeouts it scheduled #60

thorn0 opened this issue Mar 7, 2025 · 1 comment
Assignees
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested

Comments

@thorn0
Copy link
Contributor

thorn0 commented Mar 7, 2025

Describe the bug
Scripts using timeouts complete execution without waiting for the scheduled timeouts to fire. The expected delayed execution does not happen, and only the synchronous parts of the script run.

To Reproduce
Run the following code:

import { loadQuickJs } from "@sebastianwessel/quickjs";
import { readFileSync } from "fs";

const { runSandboxed } = await loadQuickJs();

const result = await runSandboxed(async ({ evalCode }) => {
  return await evalCode(`
      console.log("Hello 1"); 
      setTimeout(function() {
        console.log("Hello 2"); 
      }, 1000);
    `);
});

console.log(result);

Expected output:

Hello 1
(1 second delay)
Hello 2

Actual output:

Hello 1
(result returns immediately, "Hello 2" is never logged)

Expected behavior
The script should wait for the scheduled timeout before completing, allowing "Hello 2" to be logged after 1 second.

Desktop (please complete the following information):
Node 22.7.0, Windows 10

@sebastianwessel sebastianwessel added help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested labels Mar 8, 2025
@sebastianwessel sebastianwessel self-assigned this Mar 8, 2025
@sebastianwessel
Copy link
Owner

That is the current-expected behavior tbh.
The "correct way" would be to promisfy and await:

console.log("Hello 1"); 
await new Promise((resolve,reject)=>{
      setTimeout(function() {
        console.log("Hello 2"); 
        resolve()
      }, 1000);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants