Skip to content

[Suggestions] A new way to represent Coroutine + returns.future @future decorator ? #2560

Open
@denis-migdal

Description

@denis-migdal

Hi,

Two suggestions :

1/ A new way to represent Coroutine

I recently discovered a little h4ck to create JS async function that are executed only when awaited:

class Coroutine extends Promise { // required if JS do instanceof Promise()
    /*
    constructor( compute ) {
          super(...);
          this.#compute = compute;
    }
    */
    then(...args) {
        console.warn("start computations") // the computations to perform.
        // this.#compute();
        this.then = super.then;
        return super.then(...args);
    }
}
let c = new Coroutine()
await c; // prints "start computations"

This could be useful if pyobj2jsobj converted <coroutine> into an instance of this class, enabling to await a corouting in JS:

from browser import window

async def foo():
    print("?")

window.f1 = foo()

// in JS : await window.f1

2/ returns.future @future decorator

The returns.future python package provides 2 decorators @future and @future-safe:
https://pypi.org/project/returns/
https://returns.readthedocs.io/en/latest/pages/future.html#returns.future.Future

@future # starts execution directly when called.
async def test(arg: int) -> float:
    return arg / 2

I though it could be quite useful if it was included to Brython's stdlib.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions