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?
5
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.
1