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 7558567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:24:10+00:00 2026-05-30T12:24:10+00:00

Coming from a c# background, I’m probably looking at JavaScript from a completely wrong

  • 0

Coming from a c# background, I’m probably looking at JavaScript from a completely wrong perspective, so please bear with me.

Leaving the advantages of async aside for a minute,
let’s say I simply want to retreive a value from an SQLite database in an HTML5 page.
What I want to see is something like

var something = db.getPicture(1);

Now consider a (perhaps very naive) implementation of this:

this.getPicture(id)
{
    this.database.transaction(function(tx)
    {
        tx.executeSql('SELECT ......', null, function(tx, results)
        {
            if (results.rows.length == 1)
                return results.rows.items(0).Url; //This of course does not resturn
                                                    //anything to the caller of .getPicture(id)
        }
    },
    function(error)
    {
        //do some error handling
    },
    function(tx)
    {
        //no error
    });                     
}

First off, it’s one big mess of nested functions and second… there’s no way for me to return the result I got from the database as the value of the .getPicture() function.

And this is the easy version, what if I wanted to retreive an index from a table first,
then use that index in the next query and so on…

Is this normal for JavaScript developers, am I doing it completely wrong, is there a solution, etc…

  • 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-30T12:24:12+00:00Added an answer on May 30, 2026 at 12:24 pm

    The basic pattern to follow in JavaScript (in asynchronous environments like a web browser or Node.js) is that the work you need to do when an operation is finished should happen in the “success” callback that the API provides. In your case, that’d be the function passed in to your “executeSql()” method.

    this.getPicture = function(id, whenFinished)
    {
        this.database.transaction(function(tx)
        {
            tx.executeSql('SELECT ......', null, function(tx, results)
            {
                if (results.rows.length == 1)
                    whenFinished(results.rows.items(0).Url);
            }
        },
    

    In that setup, the result of the database operation is passed as a parameter to the function provided when “getPicture()” was invoked.

    Because JavaScript functions form closures, they have access to the local variables in the calling context. That is, the function you pass in to “getPicture()” as the “whenFinished” parameters will have access to the local variables that were live at the point “getPicture()” is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Coming from a Java background, I am a little perturbed by Ruby's completely blasé
Coming from a javascript background, collections like dictionaries are often implemented as objects because
what is wrong with this code below? The conn_PageDeleted is coming from a background
I'm coming from a background in C++ and Java, and trying to learn Javascript
I'm coming from a background where the DB schema is defined as completely as
Coming from a background, I'm familiar with GUI editors that do a poor job
Coming from a background in Django, I often use template inheritance, where multiple templates
I'm coming from a background in ColdFusion, and finally moving onto something modern, so
When I was learning Java coming from a background of some 20 years of
Coming from a C# background the naming convention for variables and methods are usually

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.