Skip to content

What is the behavior of the dynamically import function #993

Closed

Description

@imzlh

I'm trying to dynamically import a JS module

if(!import.meta.path || self.entry == import.meta.path){
    const file = self.argv[0];
    if(!file){
        throw new Error('Expect an arg to run test');
    }
    console.log('Start test:', file, '\n');

    try{
        const path = (import.meta.dirname || self.dirname) + '/' + file + '.js';
        console.log(path);
        import(path);
        delay(10000);
    }catch(e){
        console.error('Could not load test:' + file, e);
    }
}

However,the promise never come fulfilled,while the follow one works

import * as test from "./test.js"

And the follow example will block the whole program infinitely

await import(path);

The key function that would affect this is "JS_ExecutePendingJob", however, I am confused about its role.
I would appreciate it if someone could tell me its role.

Activity

  1. saghul commented on Mar 23, 2025

    @saghul
    Contributor

    Please paste a full script that exhibits the problem.

  2. bnoordhuis commented on Mar 23, 2025

    @bnoordhuis
    Contributor

    Call JS_ExecutePendingJob(ctx, &ctx1) in a loop, or call js_std_loop(ctx) when your program calls js_std_init(ctx, m).

    You can find examples of how to use both functions in this repo.

  3. locked and limited conversation to collaborators on Mar 23, 2025
  4. converted this issue into a discussion #994 on Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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