Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
274 views
in Technique[技术] by (71.8m points)

html - How to deal with async function results in JavaScript

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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...