views:

35

answers:

0

Hello,

I am trying to understand the usage of resultNotifier of Narrative JavaScript. I wrote the code below (HTML & njs).

When the code is running it displays 2 alert windows that come from these 2 instructions:

alert(notifier); ------------------------> [object Object]

alert(" value = " + value); --------> value = [object Object]

But I think that the second alert window would not have to be displayed as before the alert instruction there is a getval call that uses the value method of the notifier with the yielding operator, which should block the script until the value is provided. Instead of this, the alert it is displayed! Why does this happen?

Did I do something wrong? Could you suggest me a way to fix this so as the control flow is blocking until the result notifier is provided with a value?

I want to use this in a project where a browser receives messages from other applications, and has to block until a message is received (until data is put in a queue structure), and then to continue from the point it was blocked.

Here is the code:

narr_test.njs

load("../deploy/njs_compile.js");

function createResNot() {
    var prom = new ResultNotifier();
    return prom;
}

function setvalue (p, val) {
    var v = p.fulfill->(val);
}

function getvalue (p) {
    var v = p.value->();
    return v;
}

narr_test.html

<html>
<head>

<script src="../deploy/njs_compile.js"> </script>
<script> NjsCompiler.load("narr_test.njs"); </script>

<script>

    var notifier = spawn ( createResNot() );

    alert(notifier);

    //setvalue(notifier,4);

    var value = getvalue(notifier);

    alert(" value = " + value);

</script>

</html>
</head>