question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve `js.Promise` facade

See original GitHub issue

Currently, the facade for js.Promise is implemented as a class:

https://github.com/scala-js/scala-js/blob/main/library/src/main/scala/scala/scalajs/js/Promise.scala#L45

But in Typescript, it’s modeled as an interface, separately from the constructors:

https://github.com/microsoft/TypeScript/blob/main/lib/lib.es5.d.ts#L1446

This results in transpilation problems in ScalablyTyped, where a typescript facade wants to implement the Promise interface but instead tries to inherit from a class requiring a constructor:

https://github.com/ScalablyTyped/Converter/issues/404#issuecomment-1023659519

The relevant parts of generated code look like this:

// generated valid typescript code
export type PrismaPromise<A> = Promise<A> & {[prisma]: true}
export class Prisma__WorkspaceClient<T> implements PrismaPromise<T> {
    [prisma]: true;
...
// transpiled to Scala
@js.native
trait PrismaPromise[A] extends js.Promise[A]

@JSImport("generated-prisma-client", "Prisma.Prisma__WorkspaceClient")
@js.native
class PrismaWorkspaceClient[T] protected () extends PrismaPromise[T] {
...

compile error:

[error]      not enough arguments for constructor Promise: (executor: scala.scalajs.js.Function2[scala.scalajs.js.Function1[T | scala.scalajs.js.Thenable[T], _], scala.scalajs.js.Function1[Any, _], _]): scala.scalajs.js.Promise[T].
[error]      Unspecified value parameter executor.
[error]      L16: class PrismaWorkspaceClient[T] protected () extends PrismaPromise[T] {
[error]                                                            ^

I’m wondering if we could improve the Promise facade to be more similar to the typescript facade, which is probably also closer to the specs.

For reference, the Promise specification: https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-properties-of-the-promise-prototype-object

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
oyvindbergcommented, Jun 5, 2022

Ok, thanks. A scala implementation of Promise was absolutely not something I considered, but it’s a fair point. With that constraint I suggest we close this issue.

I suppose I can make this work on my side by analyzing the inheritance hierarchy for every class and infer an extends js.Promise(js.native, js.native) with <original inheritance>. I’ll likely skip that work and just use js.Thenable instead.

1reaction
sjrdcommented, Jun 2, 2022

The specification you linked clearly establishes that js.Promise is a class.

How do you draw this conclusion? Do you mean the first sentence?

From the existence of The Promise Constructor. If something has a constructor, it’s a class (these two ideas are one and the same thing in ES).

If it is definitely a class, then you would say that the Typescript facades are wrong?

It depends on your (and their) definition of correct or wrong. What I can tell you is that making it a trait in Scala.js would be wrong for the definition of Scala.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Improve async programming with JavaScript promises
In this guide, you'll learn some practical ways to improve asynchronous programming in JavaScript using promises, including:.
Read more >
Learn Promises – JavaScript: The New Hard Parts
Facade functions that both initiate background web browser work and in JavaScript, return a placeholder object known as a promise object immediately, ...
Read more >
Wrapping jQuery $.ajax behind a facade in q.js without ...
It seems like for the sake of one good idea (wrap jQuery's promises in Q promises) you've come up with two or three...
Read more >
The Facade pattern and applying it to React Hooks
By doing the above, we've proven that the Facade is, in general, a concept worth looking into if we want to improve the...
Read more >
Recent Advances and Improvements to JavaScript Promises
Promises, Promises. JavaScript's been slowly but steadily improving as a programming language ever since its inception back in 1995, and lately ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found