Skip to content

Can't get external async function to work #57

@BoryaGames

Description

@BoryaGames

This code:

var { Arena } = require("quickjs-emscripten-sync");
var ctx = (await QJS.getQuickJS()).newContext();
var arena = new Arena(ctx, {
  "isMarshalable": true
});
arena.expose({
  console
});
arena.evalCode(`
  (async () => {
    console.log(1);
    await new Promise(res => res("cat"));
    console.log(2);
  })();
`);

It prints 1 and after I do arena.executePendingJobs();, it works printing 2.
However, I can't get an external Promise to work.

var { Arena } = require("quickjs-emscripten-sync");
var ctx = (await QJS.getQuickJS()).newContext();
var arena = new Arena(ctx, {
  "isMarshalable": true
});
function someAsyncOperation() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve("cat");
    }, 1000);
  });
}
arena.expose({
  console, someAsyncOperation
});
arena.evalCode(`
  (async () => {
    console.log(1);
    await someAsyncOperation();
    console.log(2);
  })();
`);

It prints 1 and fails with Method Promise.prototype.then called on incompatible receiver #<Promise>
Am I'm doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions