2

I'm new comer for js program, please excuse me if some not clear but let me know the problem.

Please see my code first:

function catchDataFromDB(db, tableName, callBack) {

var sqlStr = 'SELECT * FROM ' + tableName;
var sqlData = [];
db.transaction(function(t) {
        // Query out the data
        t.executeSql(sqlStr, [], function(t, SQLResultSet) {
            var len = SQLResultSet.rows.length,
                row;
            for (var i = 0; i < len; i++) {
                row = SQLResultSet.rows.item(i);
                sqlData.push(row);
            }
            console.log(sqlData);
        });
    },
    function(SQLError) {
        console.warn(SQLError.message);
    });
 return sqlData;
}

This function is used for gets the data from WebSql, and I want it returns the sqlData for me(it's an array). The problem is it's not finished gets data from the WebSql when I call the function to get the return value, so I got nil. I think callBack function will be useful for this to get the correct data, but I don't know how to do it. Thanks for any kindly help.

Additional: I've found a method from this page, the code as below:

function doSomething(callback) {
// ...

// Call the callback
callback('stuff', 'goes', 'here');
}

function foo(a, b, c) {
// I'm the callback
alert(a + " " + b + " " + c);
}

doSomething(foo);

But how to gets the return from function foo when I call function doSomething?

Community
  • 1
  • 1
Raymond Liao
  • 1,799
  • 3
  • 20
  • 32

1 Answers1

1

callback is simple function that is invoked when some works is complete, in javascript you can pass it in variable, so in your async/worker function you have to invoke callback function (like callback(result)) once it finishes the job.

so your code should be

function catchDataFromDB(db, tableName, callBack) {
  var sqlStr = 'SELECT * FROM ' + tableName;
  db.transaction(function(t) {
      // Query out the data
      t.executeSql(sqlStr, [], function(t, SQLResultSet) {
        var len = SQLResultSet.rows.length,
          row;
        for (var i = 0; i < len; i++) {
          row = SQLResultSet.rows.item(i);
          sqlData.push(row);
        }
        callBack(null, sqlData);
      });
    },
    function(SQLError) {
      callBack(SQLError.message);
    });
}

by convention callback's first argument should be error (if you have any), the rest of arguments are results
callback(null, result1, result2,...) // when you have no error just result
callback(error) // when you have error and no result

Bek
  • 3,157
  • 1
  • 17
  • 28
  • Thanks for answer! 1. Should I need to implement the callback function? Like function returnArray (datas) { return datas; }; 2. How to get the return data? How about: var result = catchDataFromDB(db, tableName, returnArray); or var result; catchDataFromDB(db, tableName, returnArray(t){ result = t; }); – Raymond Liao Dec 28 '15 at 09:15
  • if your function is not doing nothing async there is not need in having callback, you just continue your normal operations inside your callback have look at his maybe it will be helpful http://www.tutorialspoint.com/nodejs/nodejs_callbacks_concept.htm, – Bek Dec 28 '15 at 09:18
  • also after learning callbacks I encourage you to learn JavaScript Promises , because callbacks are not idle solution for solving async code, but good to learn before moving to promises – Bek Dec 28 '15 at 09:19
  • I'm sorry, it looks difficult for me from that url you sent, can u help to make sample? I wanna to know when I call a function which have an callBack parameter, how to get the return value from the callBack function? And for JavaScript Promises, I'll do it as your encourage. – Raymond Liao Dec 28 '15 at 09:29