Bug 241088

Summary: Promise.race(123); does not throw an error
Product: WebKit Reporter: 845043157
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED INVALID    
Severity: Normal CC: saam, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: Safari 15   
Hardware: PC   
OS: Linux   

Description 845043157 2022-05-29 20:34:32 PDT
Run the program:
    Promise.race(123).then(function(value){
        console.log(value);
    });

Actual results:

(print nothing)

Expected results:
    In V8,it throws an typeerror:"TypeError:number 123 is not iterable  (cannot read property Symbol(Symbol.iterator))".
    In ES6,it writes that Promise.race() needs an iterable parameter.
    So,I think that it may be a bug.
Comment 1 Radar WebKit Bug Importer 2022-06-05 20:35:15 PDT
<rdar://problem/94415890>
Comment 2 Yusuke Suzuki 2022-06-07 03:35:59 PDT
It is V8's bug.
https://tc39.es/ecma262/#sec-promise.race
step-6 should reject the returned promise, not throwing an error.
Comment 3 845043157 2022-06-07 06:32:29 PDT
When I run the follow program:
1.js:
Promise.race(123);
print(1);

V8:
1
1.js:1: TypeError: number 123 is not iterable (cannot read property Symbol(Symbol.iterator))
Promise.race(123);
        ^
TypeError: number 123 is not iterable (cannot read property Symbol(Symbol.iterator))
    at Function.race (<anonymous>)
    at 1.js:1:9

spidermonkey:
1
Unhandled rejection: (new TypeError("Argument of Promise.race is not iterable", "1.js", 1))
Stack:
  @1.js:1:9

jsc:
1

'Promise.race(123);' is wrong code but the output of jsc looks like it is ok.I think it may be better to throw an error or warning tell users the code is wrong.
Comment 4 Yusuke Suzuki 2022-06-07 06:46:52 PDT
(In reply to 845043157 from comment #3)
> When I run the follow program:
> 1.js:
> Promise.race(123);
> print(1);
> 
> V8:
> 1
> 1.js:1: TypeError: number 123 is not iterable (cannot read property
> Symbol(Symbol.iterator))
> Promise.race(123);
>         ^
> TypeError: number 123 is not iterable (cannot read property
> Symbol(Symbol.iterator))
>     at Function.race (<anonymous>)
>     at 1.js:1:9

This V8's behavior is not aligned to the spec (spec requires rejecting the resulted promise, not throwing an error).

> 
> spidermonkey:
> 1
> Unhandled rejection: (new TypeError("Argument of Promise.race is not
> iterable", "1.js", 1))
> Stack:
>   @1.js:1:9

SpiderMonkey shell dumps Unhandled rejection too.
In JSC, you can get it via,

setUnhandledRejectionCallback(function (error) {
    // When unhandled rejection happens, this callback is called.
});

> 
> jsc:
> 1
> 
> 'Promise.race(123);' is wrong code but the output of jsc looks like it is
> ok.

You can catch the rejected promise via,

Promise.race(123).catch(function (error) {
    // Here, you will get an error in JSC / SpiderMonkey.
});



I think it may be better to throw an error or warning tell users the code
> is wrong.
Comment 5 Yusuke Suzuki 2022-06-07 06:54:11 PDT
Putting a default unhandled rejection handler, which dumps log, sounds interesting idea, though it is not compatible with the existing JSC stress tests. So probably, we should add a command line flag to switch that if we add that :)
Comment 6 845043157 2022-06-07 18:08:18 PDT
Thank you for your further explication.