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!

0

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
did this 'asynchronous' issue ever get answered?
i also have this problem. have you solved this problem if yes then plz share thanks. - Pointy and Pete Shaw
3

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.

1 Comment

In my opinion, this is the "correct" answer.
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.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.