question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to wait for the callback function?

See original GitHub issue

Hi

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:open
  • Created 9 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
tom91136commented, Aug 30, 2014

Simply using a CountDownLatch would work, or you can create a Future to make it modular…

Somthing like this using a CountDownLatch:

            final CountDownLatch latch = new CountDownLatch(1);
            final MutableObject<String> result = new MutableObject<>();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    webView.evaluateJavascript(js, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            result.setValue(value);
                            latch.countDown();
                        }
                    });
                }
            });
            latch.await();
            result.getValue();
0reactions
nouhcccommented, Jun 15, 2018

thanks a lot @tom91136

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found