V
V
vasIvas2015-08-06 18:28:23
Angular
vasIvas, 2015-08-06 18:28:23

$q, Promises or task?

All languages ​​have synchronous task libraries that I'm terribly used to.
Type -

first(/** создать объект и передать в него какие-то аргументы*/)
.then(/** получить объект полученный на предыдущем шаге и что-то с ним сделать*/)
.then(/**получить объект полученный на предыдущем шаге и что-то с ним сделать */)...

There is something similar in js in the form of Promises and Q, but I can't figure out if they are the full equivalent of what I'm talking about? That is, all the examples that I have ever met were given in the context of asynchrony, but I still did not understand whether they were sharpened specifically for asynchrony or can they always be used at all?
And if Promises and Q can be used in any "context", then what would be better in an angular context?
If there is not enough information, then here is the specifics. Suppose I have a button subscribed to a click event.
On event I have to - change color, change transparency. If I take out the method calls that will do this in the handler, then we get a rigid coupling and the inability to add something at the configuration stage. Therefore, I want to make sequential execution of tasks that can be set at the configuration stage and only task.execute () can be performed in the handler.
Will Promises and Q work for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-06
@vasIvas

Promises, awaitables, tasks... they are all one and the same - one-time operations. That is, they are not suitable for processing periodic events. And yes, in the context of angular, you can use any implementation that supports then, it will convert them into its own promises. In general, it is better to use either native promises, which are already available in all kinds of chrome, or $q from Angular.
what you describe - event stream. for this you can take https://baconjs.github.io/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question