如何处理JavaScript中的异步函数结果 [英] How to deal with async function results in JavaScript

查看:137
本文介绍了如何处理JavaScript中的异步函数结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从ac#背景来看,我可能从一个完全错误的角度看待JavaScript,所以请耐心等待。



保留async的优势一分钟,
比方说,我只是想从HTML5页面中的SQLite数据库中检索一个值。
我想看到的是类似于

  var something = db.getPicture(1); 

现在考虑一个(可能非常天真)的实现:

  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; //这当然不会重新获得
//任何东西给.getPicture(id)
}
},
函数(错误)的调用者。
{
//做一些错误处理
},
函数(tx)
{
//无错误
});

首先,它是嵌套函数的大混乱,其次......没有方式为我返回从数据库中得到的结果作为.getPicture()函数的值。



这是容易的版本sion,如果我想从表格中首先检索一个索引,那
然后在下一个查询中使用该索引,如此... ...

这是正常的JavaScript开发人员,我是否完全错误,是否有解决方案,等等...

解决方案

基本模式遵循JavaScript(在Web浏览器或Node.js等异步环境中)是,当操作完成时您需要完成的工作应该发生在API提供的成功回调中。在你的情况下,这就是传递给你的executeSql()方法的函数。

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





$ b $ p在该设置中,数据库操作的结果作为参数传递给函数在调用getPicture()时提供。



因为JavaScript函数形成闭包,所以它们可以访问调用上下文中的局部变量也就是说,传入getPicture()作为whenFinished参数的函数将有权访问在getPicture()点生效的局部变量。


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

解决方案

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.

这篇关于如何处理JavaScript中的异步函数结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆