Treating asynchronous and synchronous operations the same way

49 views
Skip to first unread message

Daniel Miladinov

unread,
Jun 2, 2014, 2:06:55 PM6/2/14
to q-con...@googlegroups.com
Bottom line up front:
Is it possible to have a promise to a result of a function that can be executed synchronously, execute synchronously, instead of being deferred to either end-of-turn or next turn?

Some More Background Information On My Question:
I have code that renders a form based on two JSON payloads - an object from JSON that represents the current value of the form (an object/map of each field and its value) and another object from JSON that represents the "type info" for each form,
how each field should render - a text input, date input, combo box, etc. Some of the options in the combo boxes should present different options depending on the value of one or more other fields. In our setup, the options for the various combo boxes
are served from other web service endpoints as indicated in the type info for each combo box field. The type info also specifies for each field, what other fields may depend on its value.

We also have a web service that accepts the current value of a form, and given those values, returns a new type info object. If a dependent field changed, for example, we'd want a new type info object so that we can rer-ender some of our combo boxes
with options from a different options url.

The form is rendered dynamically such that on every form change event, be that a combo box selection, or input into a text box, we save off the current state of the form, optionally get new type info from the server, and re-render the new form (Using React.js,
this isn't as crazy as it might sound to you otherwise). If a dependent field changed, we wrap a service call to the type info service (sending up the current type info and current form value) in a promise and upon completion of that promise and attach 
all our then() handlers to it to save off the form state and render the next version of the form. If a dependent field didn't change, we just wrap the current type info object in a Q() and run the same code on that instead.

One of the fields on the form is just a text box, and it is not a dependent field. But with the above architecture, we found that you just couldn't type into the box because even just wrapping the function (which can execute immediately) in Q() caused it to be
delayed until end of turn or next turn, and so the key presses were either delayed or even lost (if you typed very quickly). I'd like to keep the same code structure in place for both the asynchronous path as well as the synchronous path.

To Sum Up:
Is there a way to make a promise-like construct, around a value or function that returns a value, that otherwise acts like a promise, but will execute right away?

Wout Mertens

unread,
Jun 2, 2014, 4:34:01 PM6/2/14
to q-continuum@googlegroups com
Sure that's possible, but will require forking Q. I think there's actually a few promise implementations that do this, but this mix of synchronous/asynchronous code is pretty error-prone.

Wouldn't it be better to bunch up change events and process only x times/second?


--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages