[quickjs-devel] Re: Unable to await promise

  • From: Yifeng Wang <doodlewind@xxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Thu, 27 Feb 2020 11:35:12 +0800

Hi,

Thanks for the correction, promise and await works fine with the setTimeout
setup.

However this may lead to another issue: If I don't import setTimeout but
call it at runtime, a ReferenceError is expected. Outside the promise, this
error can be thrown to terminal, while inside the promise callback, no
error is thrown, neither does the typical TypeError can be reported inside
promise. I can manually catch errors in this manner:

```
function errFn () {
  return new Promise(resolve => {
    var x = null;
    x(); // TypeError
  });
}

async function f1 () {
  try {
    await errFn();
  } catch (e) {
    console.log(e);
  }
}
f1()
```

but without try-catch, errors are "swallowed". Wondering is this expected
behavior? Thanks.


Regards
Yifeng Wang


Fabrice Bellard <fabrice@xxxxxxxxxxx> 于2020年2月26日周三 下午10:45写道:

Hi,

The "setTimeout" function does not exist. You must add:

import { setTimeout } from "os";

at the start of the script.

In older versions, the std and os modules were included by default,
hence os.setTimeout() worked. You can have the legacy behavior with the
"--std" qjs option.

Best regards,

Fabrice.

On 2/26/20 2:49 PM, Yifeng Wang wrote:
Hi,

Promise is supposed to be able to use with await, while in the
2020-01-05 and 2020-01-19 build, following code does not work:

```
function resolveAfter2Seconds (x) {
   return new Promise(resolve => {
     setTimeout(() => {
       resolve(x);
     }, 2000);
   });
}

async function f1 () {
   var x = await resolveAfter2Seconds(10);
   console.log(x); // 10
}

f1();
```

Run the script, node will wait until the output, while qjs returns
immediately. The code is just the demo from MDN:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

I guess this is a regression, since I've made such usage with the
2019-10 release before.

Regards
Yifeng Wang


Other related posts: