使函数调用等待Web SQL查询 [英] Make function call wait for web SQL query

查看:127
本文介绍了使函数调用等待Web SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Primecheck函数应该返回true或false是否传递的数字是素数。如果数字是素数,函数将其添加到PRIMES表。
这是一个筛选Eratosthenes算法,但它还没有完成。

Primecheck function is supposed to return true or false whether passed number is prime or not. If the number is prime, function adds it to PRIMES table. This is a Sieve of Eratosthenes algorithm, but it's not finished yet.

function primecheck (number) {
    var isprime = true;
        if (number%10 == 1 || number%10 == 3 || number%10 == 7 || number%10 == 9) {
        db.transaction(function (tx) {
            tx.executeSql('SELECT * from Primes', [], function (tx, result) {
                for (var i = 1; i < result.rows.length; i++) {
                    if (number%result.rows.item(i)['prime'] == 0) {
                        isprime = false;
                        break;
                    }
                }
                if (isprime) {
                    tx.executeSql('INSERT INTO PRIMES (prime) values (?)', [number]);
                }
                return isprime;
            }, null);
        }, null, null);

    }
    else {
        isprime = false;
        return isprime;
    }
}

问题:通过数字,不结束在1,3,7,9,函数返回true,它的确定。
但是当我传递其他数字时,函数返回undefined。我想这是因为函数调用不会等待SQL查询完成,所以我必须使用某种回调函数。

Problem: when I pass numbers that do not end on 1, 3, 7, 9, function returns true, it's ok. But when I pass other numbers, function returns undefined. I suppose it's because function call doesn't "wait" for SQL query to finish, so I must use some kind of callback functions. But it didn't work.

推荐答案

如果你的函数执行异步操作,它不能返回一个基于结果的值异步操作。 (这是因为异步函数将不会运行,直到当前执行完成,由于JavaScript的单线程性质)。相反,你的函数应该期望一个回调函数,它接受的将是返回值作为参数。

If your function performs asynchronous operations, it cannot return a value based on the result of those asynchronous operations. (This is because the asynchronous functions won't run until the current execution is finished, due to the single-threaded nature of JavaScript.) Instead, your function should expect a callback function that takes the would-be return value as an argument.

您目前调用的函数如下:

You currently call your function like:

var isprime = primecheck(someNum);
// now do something with isprime

但是你需要使用回调函数: p>

But you need to use a callback:

primecheck(someNum, function(isprime) {
    // now do something with isprime
});

只需添加第二个回调参数,并调用该回调,而不是使用 return

Just add a second callback argument, and call that callback instead of using a return:

function primecheck (number, callback) {
    var isprime = true;
        if (number%10 == 1 || number%10 == 3 || number%10 == 7 || number%10 == 9) {
        db.transaction(function (tx) {
            tx.executeSql('SELECT * from Primes', [], function (tx, result) {
                //....
                callback(isprime);
            }, null);
        }, null, null);
    }
    else {
        isprime = false;
        callback(isprime);
    }
}

现在, primecheck 不返回任何内容,但传入 primecheck 的回调函数将以 isprime 每当 primecheck 决定输入的原始性。

Now, primecheck returns nothing, but the callback function you pass into primecheck will get fired with isprime as its first argument whenever primecheck determines the primality of the input.

这篇关于使函数调用等待Web SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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