W3C home > Mailing lists > Public > public-sysapps@w3.org > February 2013

Re: Concerns about DOMRequest

From: Jonas Sicking <jonas@sicking.cc>
Date: Thu, 14 Feb 2013 21:08:18 -0800
Message-ID: <CA+c2ei_THZCe5=pOGEA7SKT_Y-r20HSGr_neRoCf9Ecvr9QT7Q@mail.gmail.com>
To: Marcos Caceres <w3c@marcosc.com>
Cc: Anne van Kesteren <annevk@annevk.nl>, Mounir Lamouri <mounir@lamouri.fr>, public-sysapps@w3.org, Alex Russell <slightlyoff@google.com>
On Thu, Feb 14, 2013 at 4:35 AM, Marcos Caceres <w3c@marcosc.com> wrote:
> On Thursday, 14 February 2013 at 11:17, Anne van Kesteren wrote:
>> On Wed, Feb 13, 2013 at 8:27 PM, Marcos Caceres <w3c@marcosc.com (mailto:w3c@marcosc.com)> wrote:
>> > It would still really suck if you added .then() but still required everything to be wrapped in a function.
>> What do you mean by this?
> In IDB, the design of the API forces a developer to do everything synchronously, even though the IDB operation is async in the background (you have to start an operation before you can set listeners on it - so developer are forced to do everything in one go inside a function). See the following, for example:
> http://www.html5rocks.com/en/tutorials/indexeddb/todo/#toc-step2

This is simply not accurate. DOMRequest doesn't force you to do
anything synchronously that DOMFuture/Promises doesn't.
DOMFuture/Promises provides the .then function which is *very* nice
syntax sugar for handling asyncronous callbacks, especially when you
need to nest multiple asynchronous actions. But it doesn't change
anything about when any actions need to be taken.

With DOMRequest the syntax is

req = doSomeAsyncThing();
req.onsuccess = successHandler;
req.onerror = errorHandler;

or, if you want to nest async callbacks:

req = doFirstAsyncAction();
req.onsuccess = function(e) {
  var req2 = doSecondAsyncAction();
  req2.onsuccess = secondSuccessHandler;
  req2.onerror = errorHandler;
req.onerror = errorHandler;

If you don't want to set up your success handler immediately, with
DOMRequest you'd do something like:

req = doAsyncAction();
doSometimeLater(function() {
  if (req.readyState == "done") {
  else {
    req.addEventListener("success", function(e) {

The equivalent snippets using DOMFuture/Promises would be something like:

doSomeAsyncThing().then(successHandler, errorHandler);

Nest async callbacks:

.then(doSecondAsyncAction, errorHandler)

Set up your success handler later:

future = doAsyncAction();
doSometimeLater(function() {

As you can see, the .then function on DOMFuture/Promises certainly
makes the code cleaner and removes a lot of the syntax overhead. But
the capabilities are exactly the same.

This is not to say that the .then function isn't important. It makes a
huge difference. But if you have other problems with DOMRequest then
those concerns likely applies to DOMFuture as well.

/ Jonas
Received on Friday, 15 February 2013 05:09:17 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 20:36:11 UTC