{"action":"created","issue":{"url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/347","labels_url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/347/labels{/name}","comments_url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/347/comments","events_url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/347/events","html_url":"https://github.com/litehelpers/Cordova-sqlite-storage/issues/347","id":106728114,"number":347,"title":"asynchronous to synchronous?","user":{"login":"tyrtok","id":12230254,"avatar_url":"https://avatars.githubusercontent.com/u/12230254?v=3","gravatar_id":"","url":"https://api.github.com/users/tyrtok","html_url":"https://github.com/tyrtok","followers_url":"https://api.github.com/users/tyrtok/followers","following_url":"https://api.github.com/users/tyrtok/following{/other_user}","gists_url":"https://api.github.com/users/tyrtok/gists{/gist_id}","starred_url":"https://api.github.com/users/tyrtok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tyrtok/subscriptions","organizations_url":"https://api.github.com/users/tyrtok/orgs","repos_url":"https://api.github.com/users/tyrtok/repos","events_url":"https://api.github.com/users/tyrtok/events{/privacy}","received_events_url":"https://api.github.com/users/tyrtok/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-09-16T08:49:04Z","updated_at":"2015-09-16T09:37:02Z","closed_at":null,"body":"Hi Im new to this. \r\n\r\nI would like to raise a question if possible. \r\nI realized that the executesql() calls are asynchronous. \r\n\r\nlets assuming you are building an app and you query the local sqlite for a certain value. is there any way to wait for the query to finish(outside of transaction scope)?\r\nvar data =\"\";\r\ndb.transaction(function (tx) {\r\n   tx.executeSql(yourquery, [], function(tx, rs){\r\n   data = rs[0] etc;\r\n }\r\n\r\nif (data == something ) {\r\n\r\n../  process the data here\r\n}\r\n});\r\n\r\n####\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/comments/140687038","html_url":"https://github.com/litehelpers/Cordova-sqlite-storage/issues/347#issuecomment-140687038","issue_url":"https://api.github.com/repos/litehelpers/Cordova-sqlite-storage/issues/347","id":140687038,"user":{"login":"brodybits","id":1559888,"avatar_url":"https://avatars.githubusercontent.com/u/1559888?v=3","gravatar_id":"","url":"https://api.github.com/users/brodybits","html_url":"https://github.com/brodybits","followers_url":"https://api.github.com/users/brodybits/followers","following_url":"https://api.github.com/users/brodybits/following{/other_user}","gists_url":"https://api.github.com/users/brodybits/gists{/gist_id}","starred_url":"https://api.github.com/users/brodybits/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brodybits/subscriptions","organizations_url":"https://api.github.com/users/brodybits/orgs","repos_url":"https://api.github.com/users/brodybits/repos","events_url":"https://api.github.com/users/brodybits/events{/privacy}","received_events_url":"https://api.github.com/users/brodybits/received_events","type":"User","site_admin":false},"created_at":"2015-09-16T09:37:02Z","updated_at":"2015-09-16T09:37:02Z","body":"Unfortunately there is no way for a piece of ES3/ES5-complaint Javascript code to simply stop and wait for sqlite results, or any of the other Cordova operations to finish since Cordova makes all native operations work asynchronously with callbacks. It does forces you to think about programming a bit differently, if you want to use ES3/ES5. (ES6 generators and LiveScript try to solve this problem. Also, I find the CoffeeScript syntax as well as the ES6 fat arrow to make it easier to deal with asynchronous code.)\r\n\r\nYou may want to check out Javascript Promises. Promises is a Javascript API that makes this kind of asynchronous programming easier for some programmers. There are some libraries out there, and IIRC Cordova does use a Promise library internally (not sure if it is available for your app code). A couple of resources that may help you:\r\n- http://ngcordova.com/docs/plugins/sqlite/ (part of the ngCordova/Ionic framework, not sure if you can isolate and use this code by itself)\r\n- https://github.com/MetaMemoryT/websql-promise - only works for Web SQL, should be very easy to adapt to work with this plugin"}}