Skip to content

await 语法支持有问题,会阻塞后续逻辑 #16

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

Open
2houyu opened this issue Sep 6, 2021 · 4 comments
Open

await 语法支持有问题,会阻塞后续逻辑 #16

2houyu opened this issue Sep 6, 2021 · 4 comments

Comments

@2houyu
Copy link

2houyu commented Sep 6, 2021

测试代码如下:

jsContext.executeVoidScript("console.log(\"Hello World\");\n" +
                                "function testAsync(params) {\n" +
                                "    console.log(\"test Async start\");\n" +
                                "    return new Promise(function (resolve, reject) {\n" +
                                "        resolve(\"done\");\n" +
                                "    });\n" +
                                "}\n" +
                                "\n" +
                                "async function main(){\n" +
                                "    var t = await testAsync(11);\n" +
                                "    console.log(\"test finish\"+t);\n" +
                                "}\n" +
                                "main();",null);

日志输出如下,test finish 没有输出
D/QuickJS: Hello World
D/QuickJS: test Async start

@taoweiji
Copy link
Owner

QuickJS对Promise的支持有问题,我这边研究一下,也欢迎你一起研究。

@2houyu
Copy link
Author

2houyu commented Sep 10, 2021

被加到pendingjob里了,参考qjs.c源码,里面有个事件循环,js_std_loop来处理这些job

@peienliudao
Copy link

我也遇到了这个问题,使用Promise不行,请问有什么解决办法吗?

@sytuzhouyong
Copy link

sytuzhouyong commented Mar 29, 2023

在执行完js后,增加以下处理,可以解决

// should executing pending promise jobs.
JSContext *pctx;
int finished = JS_ExecutePendingJob(runtime, &pctx);
while (finished != 0) {
        finished = JS_ExecutePendingJob(runtime, &pctx);
        if (finished == -1) {
            break;
        }
}

@peienliudao @2houyu

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

No branches or pull requests

4 participants