Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to pass a javascript callback correctly? #81

Open
smstuebe opened this issue May 22, 2023 · 1 comment
Open

[Question] How to pass a javascript callback correctly? #81

smstuebe opened this issue May 22, 2023 · 1 comment

Comments

@smstuebe
Copy link

Hi,

Is it possible to pass a javascript function as callback? (the feature table suggests it)
Somehow my script hangs when doing this:

cat.py

class Cat:
    def __init__(self, name):
        self.name = name  

    def meow(self, x):
        return x(self.name)

test.ts

const t = await python('../cat.py');

const cat = await t.Cat('test');
console.log(await cat.name); // outputs test
const cb = async (s: string) => s.split('').reverse().join('');
console.log(await cat.meow(cb)); // hangs

I'm using typescript.
I couldn't find an example how to do it right. I tried different variations auf using async / await. Sorry if I have overseen it.

Thanks

rgds Sven

@extremeheat
Copy link
Owner

def fn(cb): cb(1, 2, 3)
const { fn } = await python('../cat.py');
await fn(console.log)

Should print out 1, 2, 3.

image

https://colab.research.google.com/gist/extremeheat/cab8f57d7f79fef37ce76ecdd5d17093/jspy-cb-test.ipynb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@smstuebe @extremeheat and others