2

XCode has webkit built in, and XCode can issue a JavaScript command and receive a return value. All that is good - except when JavaScript has a callback function like with executeSql.

How do you write a function that doesn't return until the callback has been called?

Do you wrap it in another function maybe?

asked Mar 12, 2013 at 1:49
2
  • you might want to look into web workers. See html5rocks.com/en/tutorials/workers/basics Commented Mar 12, 2013 at 1:52
  • You wrote a game called thermonuclear war, and there is a course at Udacity put on by Google developers that is talking about game development. Just thought you'd be interested. Commented Mar 12, 2013 at 3:11

2 Answers 2

2

There are two solutions - you may either write your entire program in continuation passing style or you may use trampolines to simulates real continuations.

If you want to use continuation passing style then I suggest you first read the following StackOverflow thread: What's the difference between a continuation and a callback?

Continuation passing style can be a pain to write. Fortunately there are JavaScript preprocessors like jwacs (Javascript With Advanced Continuation Support) which ease writing such code: http://chumsley.org/jwacs/

The second option (using trampolining) currently only works in Firefox and Rhino. Sorry XCode. You can read more about trampolining here: Trampolines in Javascript and the Quest for Fewer Nested Callbacks

If it interests you then I've written a small fiber manager for JavaScript that allows you to call asynchronous functions synchronously: https://github.com/aaditmshah/fiber

answered Mar 12, 2013 at 2:05
Sign up to request clarification or add additional context in comments.

1 Comment

I think I'm in a catch 22. Firefox doesn't support executeSql I don't think. There's a lot for me to learn here.
1

May I suggest checking it periodically?

var executeSqlIsDone = false;
executeSql({
 callback: someCallbackFunction();
 });
waitUntilCallbackIsFinished();
//continue processing
function someCallbackFunction()
{
 executeSqlIsDone = true;
}
function waitUntilCallbackIsFinished()
{
 if(executeSqlIsDone === false)
 {
 setTimeout(waitUntilCallbackIsFinished, 100); //some low value
 }
 //else - do nothing. Wait.
}

Also look into

answered Mar 12, 2013 at 2:07

1 Comment

Spinlocking is definitely the way to go. =)

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.