-1

I'm using the StratifiedJS library's waitFor construct.

function myFunction() {
     // some declarations
      waitfor() {
            AsyncService.getThisDone().then(function(result) {
                // some more calculation
                resume();
            });
      }
}

I am getting unexpected '{' on the line where waitfor is used as I cannot enclose the above code in <script type="text/sjs"> tag.

How can I get this working in AngularJS

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Nitin
  • 7,187
  • 6
  • 31
  • 36
  • 2
    What library are you using that defines `waitfor` and `resume` as global functions? – Sukima Jun 28 '14 at 22:55
  • @Sukima Got these keyword from http://onilabs.com/stratifiedjs library. Documentation for waitfor, found here - https://conductance.io/reference#sjs:%23language/syntax::waitfor-resume – Nitin Jun 28 '14 at 22:57
  • Then your question is not about JavaScript but about StratifiedJS which seems (according to the front page) to be a transpiler or alternative interpreter. – Sukima Jun 28 '14 at 23:04
  • 1
    Please don't punish (down vote) the answers for JavaScript when your question did not specify StratifiedJS at the time people were trying to answer the question. – Sukima Jun 28 '14 at 23:08
  • @Sukima: I don't think Nitin can have downvoted any answers (and it's always dangerous to assume; votes are anonymous). Downvoting [requires >=125 rep](http://stackoverflow.com/help/privileges). – T.J. Crowder Jun 28 '14 at 23:20
  • @T.J.Crowder, good point. Still not sure why the JS answers were considered a downvote. True they were offtopic but only till several edits and much confusion later. Hence why I deleted mine once it became offtopic. – Sukima Jun 28 '14 at 23:22

3 Answers3

2

The library you linked says you need to include this on the main page:

 <script type="text/sjs">
 // Your SJS code here!
 </script>

Make sure you've labelled it that way. Most javascript is labelled as "text/javascript" (or has no label at all, in which case the <script> tag implies javascript.

The code you posted runs fine when typed in the command-line-like eval tool they have on the StratifiedJS page - likely there is nothing wrong with the code itself, just the way it's loaded.

blgt
  • 8,135
  • 1
  • 25
  • 28
  • @Nitin You write the script tags in the html bit. I'm guessing you have something like `` in the header of your webpage; just change it to `` – blgt Jun 28 '14 at 23:16
2

As @blgt said, you need to put the SJS code inside an appropriate <script type="text/sjs"> tag (since most StratifiedJS code is not valid JavaScript).

In order to access SJS functions from plain JS frameworks like SJS, you could attach them to some well known object, e.g:

<script type="text/sjs">
window.myFunction = function() {
  waitfor() {
    AsyncService.getThisDone().then(function(result) {
      // some more calculation
      resume();
    });
  }
}
</script>

You can then call window.myFunction from any JS code. In real code you'd probably pick a more unique name ;)

Note: it's impossible to actually wait for the execution of a suspending StratifiedJS function like this if you call it from JS. So if you're calling StratifiedJS functions from JS, generally they should be fire-and-forget (i.e functions which are only executed for their side-effects, rather than functions which return a result).

This blog post has some details about integrating AngularJS and StratifiedJS, which may clarify the relation between them:

http://onilabs.com/blog/megazine

gfxmonk
  • 8,614
  • 5
  • 42
  • 53
-1

The short version is that your JavaScript syntax is invalid.

The medium version is that

functionCall() {
    // stuff here
}

...is invalid. You'd need a ; after functionCall(), and if you had one, the block following it would have no purpose.

Given that your linked documentation suggests that it is valid, there must be some pre-processing step that turns the code described into valid JavaScript that you've skipped. So your code is being processed as normal JavaScript, in which this construct is not valid.

The solution is to ensure that this code is pre-processed and/or run in the right environment (I think it's by their Conductance server).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I tried to explain that in my answer as well but according to StratifiedJS `waitFor()` is valid syntax. Suggesting quite strongly that StratifiedJS is not JavaScript. – Sukima Jun 28 '14 at 23:06
  • @Sukima: Right. As I said, it needs pre-processing/processing in the correct environment. – T.J. Crowder Jun 28 '14 at 23:16