How to wait for the callback function?
See original GitHub issueHi
do you know a good way to turn the event driven programming into non-event driven. Lets say you want to define a function like the following example:
protected String getDate() {
mJsEvaluator.evaluate("new Date().toString()", new JsCallback() {
@Override
public void onResult(final String resultValue) {
Log.d("Date", String.format("Result: %s", resultValue));
// Block until event occurs.
}
});
return "value of resultValue";
}
I have read some code in your test app:
@MediumTest
public void testEvaluateJQuery() throws InterruptedException {
final TextView resultTextView = (TextView) mActivity
.findViewById(R.id.realLibraryResultJqueryView);
final String expectedResult = "Result: jQuery is working!";
for (int i = 0; i < 100; i++) {
Thread.sleep(100);
if (resultTextView.getText().equals(expectedResult)) {
break;
}
}
assertEquals(expectedResult, resultTextView.getText());
}
Is this the way to go?
Issue Analytics
- State:
- Created 9 years ago
- Comments:9 (3 by maintainers)
Top Results From Across the Web
How to make a function wait until a callback has been called ...
// waits let value = await new Promise((resolve) => { someEventEmitter.once('event', (e) => { resolve(e. ... value); }); });.
Read more >JavaScript wait for function to finish tutorial - Nathan Sebhastian
Modern JavaScript allows you to wait for a function to finish before executing the next piece of code. While the callback pattern works,...
Read more >How to Wait for a Function to Finish in JavaScript - Linux Hint
Method 1: Using a Callback Function With setTimeout() to Wait for a Function to Finish · The “first()”, “second()” and “third()” methods are...
Read more >JavaScript — from callbacks to async/await - freeCodeCamp
Asynchronous requests will wait for a timer to finish or a request to respond while the rest of the code continues to execute....
Read more >Wait for a Function to Finish in JavaScript | Delft Stack
Another way to wait for a function to execute before continuing the execution in the asynchronous environment in JavaScript is to use async/wait...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Simply using a
CountDownLatch
would work, or you can create aFuture
to make it modular…Somthing like this using a
CountDownLatch
:thanks a lot @tom91136