7

Consider this code (shortened)

function getSecret() {
    db.transaction(
        function (transaction) {
            transaction.executeSql(
                'SELECT * FROM table LIMIT 1;',
                null,
                function(transaction, result) {
                    var row = result.rows.item(0);
                    var secret = row.secret;
                    return secret;
                }, errorHandler
            );
        }
    )
}

How would I return the value of secret to the main function? I have read this answer Return value from nested function in Javascript

And tried this

function getSecret() {
    db.transaction(
        function doSql(transaction) {
            transaction.executeSql(
                'SELECT * FROM table LIMIT 1;',
                null,
                function(transaction, result) {
                    var row = result.rows.item(0);
                    var secret = row.secret;
                    return secret;
                }, errorHandler
            );
        }
    )
    return doSql;
}

However this did not work.

Thanks!

Community
  • 1
  • 1
Pete
  • 485
  • 1
  • 5
  • 10

3 Answers3

4

Try:

function getSecret() {
    var secret = '';

    db.transaction(
        function (transaction) {
            transaction.executeSql(
                'SELECT * FROM table LIMIT 1;',
                null,
                function(transaction, result) {
                    var row = result.rows.item(0);
                    secret = row.secret;
                }, errorHandler
            );
        }
    )

  return secret;
}
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 19
    Is the database operation not asynchronous? I'm pretty sure that it is, and that therefore the solution here cannot possibly work. The "success" callback function is called when the query completes, while the outer function will return immediately after the query is *started*. – Pointy Jan 24 '12 at 14:00
  • did this 'asynchronous' issue ever get answered? – Black Apr 03 '13 at 11:36
  • 1
    i also have this problem. have you solved this problem if yes then plz share thanks. - Pointy and Pete Shaw – Manish Agrawal Jun 27 '13 at 08:19
1
function getSecret() {
    var retval = undefined; // or a reasonable default
    db.transaction(
        function doSql(transaction) {
            transaction.executeSql(
                'SELECT * FROM table LIMIT 1;',
                null,
                function(transaction, result) {
                    var row = result.rows.item(0);
                    var secret = row.secret;
                    retval = secret;
                }, errorHandler
            );
        }
    )
    return retval;
}

This will create a closure over the retval variable. When transaction is called, retval will be updated.

Fletcher Moore
  • 13,558
  • 11
  • 40
  • 58
  • 9
    This will not work. The callback function from the query is executed asynchronously, while the return from `getSecret()` will happen immediately after the transaction is *started*. – Pointy Jan 24 '12 at 13:59
1

Try promisifying the database operation before returning your payload.

Additionally, you can use promise's built in reject() as an error handler, returning the transaction object whenever the function returns undefined.

Declare promise within function:

function getSecret() {
    var secret = '';
    return new Promise(function(resolve, reject) {
        db.transaction(
            function(transaction) {
                transaction.executeSql(
                    'SELECT * FROM table LIMIT 1;',
                    null,
                    function(transaction, result) {
                        if (typeof(result) === 'undefined') {
                            var row = result.rows.item(0);
                            secret = row.secret;
                            resolve(secret)
                        },
                        else {
                            reject(transaction)
                        }
                    }
                );
            }
        )
    })
}

Then call the function.

//If function resolves, log the secret. 
//if function rejects, log the transaction
getSecret().then(function(secret) {
    console.log(secret);
})
.catch(function(err) {
    console.log('Error in db.transaction ' + err)
})

Hope that helps.