Skip to content

proposal: syscall/js: ValueOf should accept []js.Value and map[string]js.Value as analogs of []any and map[string]any #60335

Open
@Jorropo

Description

@Jorropo

ValueOf right now converts js.Value as [its value].

So I naively assumed I could write this code:

// racyAwait takes in multiple promises and race them against each other using Promise.race
func racyAwait(ctx context.Context, promises ...js.Value) (sucess []js.Value, err error)
  return await(ctx, js.Global().Get("Promise").Call("race", promises))
}

However it doesn't, I have to write this instead:

// racyAwait takes in multiple promises and race them against each other using Promise.race
func racyAwait(ctx context.Context, promises ...js.Value) (success []js.Value, err error)
  r := make([]any, len(promises))
  for i, p := range promises {
    r[i] = p
  }
  return await(ctx, js.Global().Get("Promise").Call("race", r))
}

This just looks silly, I guess it also have a performance impact but it's not like syscall/js.ValueOf is particularly fast anyway.


I propose this change to it's behaviour:

 | Go                     | JavaScript             |
 | ---------------------- | ---------------------- |
 | js.Value               | [its value]            |
 | js.Func                | function               |
 | nil                    | null                   |
 | bool                   | boolean                |
 | integers and floats    | number                 |
 | string                 | string                 |
 | []interface{}          | new array              |
+| []js.Value             | new array              |
 | map[string]interface{} | new object             |
+| map[string]js.Value    | new object             |

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions