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

Promises #9

Open
tjvr opened this issue Jul 27, 2017 · 5 comments
Open

Promises #9

tjvr opened this issue Jul 27, 2017 · 5 comments

Comments

@tjvr
Copy link
Member

tjvr commented Jul 27, 2017

Proposal: forever and Sound.play should return a Promise. That way, you can do stuff after they finish.

Example for forever:

forever(() => {
  player.x += 1
  if (player.left > world.width) {
    return false // stop forever
  }
}).then(() => {
  player.y += 1
  // ...
})

sound.play().then(() => { … }) would act like "play sound and wait" from Scratch.

My concern is that introducing Promises really complicates the concept of control flow—because they're a poor fit for the nature of JavaScript! Using await/async might help alleviate this, but then we're introducing more confusing keywords.

Help!

@bates64
Copy link

bates64 commented Aug 2, 2017

I'd argue that async/await is less confusing - especially if you don't know that they're actually promises. If everything is async/await it simplifies a lot of code.

await UW.init(...)
let player = (...)

await waitSeconds(5) // etc

@tjvr
Copy link
Member Author

tjvr commented Aug 3, 2017

Interesting thought. Can await/async be used at the top-level scope in an ES module?

@bates64
Copy link

bates64 commented Aug 4, 2017

No.

You'd have to do:

import 'you-win' as UW

async function main() {
  await UW.init()
  // ...
}

main().catch(console.error)

Which I'd argue is fairly ugly. You could do:

import 'you-win' as UW

UW.init(...).then(async () => {
  // ...await foo()
}).catch(console.error) // not sure if you need this in browsers?

Which is almost the same as the current recommended way of initialising, except it has an async keyword.

@bates64
Copy link

bates64 commented Aug 4, 2017

Or, if the game itself could be considered a module (this allows us to easily have an actual restart() method, too!)

import 'you-win' as UW

export async function main() {
  await UW.init()
  // ...
}

And then we somehow dynamically import that script (babel in the browser??) and execute main().
Perhaps you could run it in a Web Worker - and kill the web worker upon restart()!

@tjvr
Copy link
Member Author

tjvr commented Aug 5, 2017

No.

Aw. :(

An export is an interesting idea, though I don't think it's required in order to have a restart() method...

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

No branches or pull requests

2 participants