5

So as you know the context: DOM with JavaScript, even though I think it is mostly a language agnostic issue (I know I put both language-agnostic and JavaScript tags, but it's only to let you see something I might not be able to see for myself).

I recently decided to use a promise mechanism to solve interactions with the user (clicks, keyboard input), and I was wondering if I was missing the point of promises.

For instance the algorithm would look like this

  • launch main menu (function that returns a promise)
  • then (takes a param input, sets some external data according to the input)
  • then (waits for a series of clicks)
  • then (navigate to another menu, also returning a promise)
  • catch (something was rejected -> then launch everything again from start)

It is a cycle, meaning that if it fails somewhere, it starts all again. I'm wondering if made a total idiot of myself with this decision or if I'm actually doing promises right, or almost but not quite.

Can you enlighten me please?

  • Promises? Can the rest of the program execute while you're waiting on the user to finish filling out that form? If it can, a Web Worker might be more appropriate. – Robert Harvey Feb 27 '15 at 17:30
  • No I have to admit that these promises have a tight coupling with the whole thing. It's a game wrapped in a promise chain to be more specific – axelduch Feb 27 '15 at 17:34
  • I wanted to approach promises like a scenarii manager where it's kind of a binary tree, meaning -> "it worked, go to the next step" or on the contrary "it failed, start all over again." – axelduch Feb 27 '15 at 17:36
  • @aduch as sort of an aside, you might find this tutorial on reactive programming interesting -- it has some examples (and mentions promises) – paul Feb 27 '15 at 19:45
  • @paul it seems like I made a naïve implementation of this paradigm. Even though it looks a bit similar to the idea behind what I am trying to achieve I suppose it is way subtler than what I actually did. I'll dig into this, thanks for the material, any other advices are welcome. – axelduch Feb 27 '15 at 19:54
3

For what its' worth, the article you linked was discussing people who write promise libraries, not people who use them.

Your use case seems a bit larger than typical, but as far as I can tell from your description, it's a relatively good fit for promises. Promises are designed to simplify sequencing and composition of asynchronous code. That's certainly your situation here. You're expecting several asynchronous actions in sequence, several of which might have subsequences.

Just make sure it's actually simplifying your code. If you find yourself in a situation where you're having a lot of difficulty getting a promise to work, use something else for that part of the code.

  • Yes, to make sure I did not misunderstand promises I wanted to dig into material for implementing one's own library according to promise-aplus-tests because I didn't feel very confident about my seeing the big picture. – axelduch Feb 27 '15 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.