The steps are as follows:
1, executeSql() is wrappered in promise
2, run promise( ) , then run batch( )
There was a problem with the operation:
in the database, the data insert success, but no console or alert.
if don't use promise async, sqlBatch() callback is normal
let promise = new Promise(resolve=>{
db.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function(rs) {
console.log('Record count (expected to be 2): ' + rs.rows.item(0).mycount);
resolve(true);
}, function(error) {
console.log('SELECT SQL statement ERROR: ' + error.message);
});
})
function batch(){
db.sqlBatch([
'CREATE TABLE IF NOT EXISTS DemoTable (name, score)',
[ 'INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101] ],
[ 'INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202] ],
], function() {
console.log('Populated database OK');
alert("success")
}, function(error) {
console.log('SQL batch ERROR: ' + error.message);
alert("error")
});
}
promise().then(res=>{
if(res === true) batch();
// batch run success, but no console or alert.the callback is invalid.
})
/ * If only batch() is executed , the callback is very good suach as */
batch(); // batch run success ,console and alert is valid
The steps are as follows: 1, executeSql() is wrappered in promise 2, run promise( ) , then run batch( )
There was a problem with the operation: in the database, the data insert success, but no console or alert. if don't use promise async, sqlBatch() callback is normal