1

I need to run a SQL SELECT statement and store the results. Then I need to pass those results into a function and create a graph based on the data points given. See code below.

var dataKWhr = getCoverageAndKWhr();
console.log(dataKWhr);
createGraph(dataKWhr);
console.log("Created graph");

The getCoverageAndKWhr function starts running but the rest of the original function continues with the log statement and createGraph. This fails because dataKWhr is undefined at this point since its value has not been returned from getCoverageAndKWhr().

I'd rather not put in a delay of a fixed number of seconds. Is there a way to wait for the first line to finish before proceeding?

This is a PhoneGap application with jQueryMobile. Currently testing on Android.

asked Oct 29, 2013 at 22:48
3
  • 1
    You will need to pass a callback function to getCoverageAndKWhr() in order for this to work the way you want. Commented Oct 29, 2013 at 22:52
  • If getCoverAndKWhr has any asynchronous code in it, you will need a callback function as @ErrolFitzgerald said. Commented Oct 29, 2013 at 23:21
  • possible duplicate of getting a Webkit executeSql transaction to return a value Commented Dec 7, 2013 at 23:10

3 Answers 3

2

It's hard to tell what really going on without the code behind the getCoverageAndKWhr function.

Maybe some execution path doesn't return a value.

getCoverageAndKWhr could also be executed asynchronously. If it's the case, you must either find a way to transform getCoverageAndKWhr into a synchronous call or pass a callback function that will be invoked once the execution of getCoverageAndKWhr has completed.

answered Oct 29, 2013 at 23:13
Sign up to request clarification or add additional context in comments.

1 Comment

Thx, I needed a callback function.
1

Any I/O operation is going to be async, for certain. Any calls to SQLite/WebSQL DB's will have callbacks as part of their construction. You'll need to make use of them. (And then, when you learn to hate the callback design pattern, you may wish to convert to Promises.)

answered Oct 29, 2013 at 23:34

Comments

1

I've found that when you're programming in Javascript, it's easier to just always assume that every function is asynchronous. That's why stuff like this is a no-no:

var dataKWhr = getCoverageAndKWhr();
createGraph(dataKWhr);

You need to use callbacks, it's the only realistic way to make sure that your variable will populate before it's used. So it should look more like this:

getCoverageAndKWhr(createGraph);

And I don't know what's in getCoverageAnKWhr, but if you're making an ajax call in there it would look something like this:

function getCoverageAndKWhr(callBack) { 
 $.ajax({
 type: 'GET',
 //whatever else
 success: function (data) { 
 callback(data.KWhr); 
 }
 });
}
answered Oct 29, 2013 at 23:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.