Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:32:01+00:00 2026-05-27T14:32:01+00:00

While developing a mobile PhoneGap app I had an interesting problem. I needed to

  • 0

While developing a mobile PhoneGap app I had an interesting problem. I needed to query about 10 items of data from the database (through PhoneGaps SQLite API)… Like many javascript API’s, this one was asynchronous. When you made your query, you would pass in a “success” handler.

Now, my preference in this case would have been a synchronous query method that returned only when complete. Then I could have written straight line code that queried each of the 10 items 1 after another.

Because of the asynchronous nature of PhoneGap (really, I see this all over JS however) I was forced to write a beast that looked like this:

db.query( "SELECT...", success() {
    db.query( "SELECT...", success() {
        db.query( "SELECT...", success() {
            db.query( "SELECT...", success() {
                db.query( "SELECT...", success() {
                }
            }
        }
    }
}

And this is only half as deep as I had to go (and greatly simplified…)… When, had I been using SQLite in C, I could have simply done something like:

db.query( "SELECT...", resultA );
db.query( "SELECT...", resultB );
db.query( "SELECT...", resultC );
db.query( "SELECT...", resultD );
db.query( "SELECT...", resultE );

It seems to me that the success handler approach is great when you only have to go 1 or 2 levels deep… But, completely falls apart when you need more than that…

Is their a library, or feature of a library somewhere that makes this easier?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:32:02+00:00Added an answer on May 27, 2026 at 2:32 pm

    This is a problem that is so prevalent in the community that many patterns and libraries have arisen to combat it.

    My favorite is promises. I gave a presentation on promises as a solution to the async problem at a few events; you can check out my slides on SlideShare: Callbacks, Promises, and Coroutines (oh my!): The Evolution of Asynchronicity in JavaScript. It also explains why asynchronicity is necessary—in short, because JavaScript is single-threaded.

    For the particular example you give, check out slide 53 and thereabouts. In brief, assuming db.query returned a promise, it would look like:

    db.query("SELECT...")
      .then(function (a) {
        return db.query("SELECT..." + a);
      })
      .then(function (b) {
        return db.query("SELECT..." + b);
      })
      .then(function (c) {
        return db.query("SELECT..." + c);
      })
      .then(function (d) {
        return db.query("SELECT..." + d);
      })
      .then(function (e) {
        return db.query("SELECT..." + e);
      });
    

    Of course it becomes a lot nicer if you don’t need to use the results of one query for the next:

    Q.all([
      db.query("SELECT..."),
      db.query("SELECT..."),
      db.query("SELECT..."),
      db.query("SELECT..."),
      db.query("SELECT...")
    ]).spread(function (a, b, c, d, e) {
      // ...
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an HTML5 app using PhoneGap + JqueryMobile. My problem is when I
While developing a C++ application, I had to use a third-party library which produced
While developing my app I have come to realize that the majority of my
While developing a new query at work I wrote it and profiled it in
While developing a firefox extension, I create a wizard window from overlay.js using: this.wizard
While developing a Grails 1.0.5 app I'm appalled at how slow the grails test-app
while developing an application how to get current operating system version installed in mobile
what to use for database , while developing J2ME apps ? I mean should
I'm developing a web based mobile application and I was thinking about the default
While developing a Rails app, I usually leave the dev server running locally (mongrel)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.