2

I'm using https://github.com/and-who/react-p5-wrapper to have p5 in my React file. I've a question about doing things asynchronously.

So in my Sketch file, instead of

p5.keyPressed = () => {
...
...
bunch of chaining setTimeout (..., 1000)
}

Can I do,

async p5.keyPressed = () => {
...
...
await p5.delay(1000)
}

What's the right syntax to do this?

Vennsoh
  • 4,853
  • 5
  • 26
  • 41
  • 2
    you await something that returns a Promise - so `const delay=ms=>new Promise(r=>setTimeout(r, ms))` - then you can `await delay(1000)` ... but it'd be `p5.keyPressed = async () =>` not what you wrote – Bravo Nov 24 '20 at 05:51
  • 1
    Oh nice, so instead of `p5.keyPressed = () => ` it should be `p5.keyPressed = async () =>` thanks! – Vennsoh Nov 24 '20 at 05:57
  • 2
    well, yes, because the function needs to be async ... not the property (which is a syntax error) – Bravo Nov 24 '20 at 05:57

0 Answers0