Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'use nodent-promises' not working ("No 'use nodent' directive, assumed -es7 mode") #3

Closed
mbrowne opened this issue Apr 3, 2015 · 6 comments

Comments

@mbrowne
Copy link

mbrowne commented Apr 3, 2015

I tried using the "use nodent-promises"; directive as described in the documentation, but nodent.js doesn't seem to recognize it:

No 'use nodent' directive, assumed -es7 mode

My goal is to get this test code working:

async function tellYouLater(sayWhat) {  
    return new Promise(function($return, $error) {
        setTimeout(function() {
            $return(sayWhat);
        }, 500);
    });
}

async function test() {
    return await tellYouLater("Hi there");
}

console.log(await test());

In the docs it says, "If you want your code to remain compatible with standard ES7 implementations when the arrive, use the second form above", referring to the new Promise syntax. I tried that with the "use nodent-es7" directive, but that output an empty object rather than "Hi there". Ideally I would like to get this working with "use nodent-es7" since I want my code to work in the browser as well -- is there no way to do that and remain future-compatible with ES7 (i.e. without a hard dependency on nodent)?

P.S. Thanks for this great library; I have been hoping for quite a while that someone would write such a library and only just now discovered it!

@MatAtBread
Copy link
Owner

I'm assuming you're trying to get this to work from the command line. I took the code above above and pasted it into xxx.js, before typing:

./nodent.js --out xxx.js

..which generated the output:

matt@matt-dev14:~/git/nodent$ ./nodent.js --out xxx.js
/* /home/matt/git/nodent/xxx.js: No 'use nodent' directive, assumed -es7 mode */
function test() {
    return function($return, $error) {
        return tellYouLater("Hi there")(function($await_tellYouLater$1) {
            return $return($await_tellYouLater$1);
        }.$asyncbind(this, $error), $error);
    }.$asyncbind(this, $error);
}

function tellYouLater(sayWhat) {
    return function($return, $error) {
        return $return(new Promise(function($return, $error) {
            setTimeout(function() {
                $return(sayWhat);
            }, 500);
        }));
    }.$asyncbind(this, $error);
}

return test()(function($await_test$2) {
    console.log($await_test$2);
}.$asyncbind(this, $error), $error);

To suppress the warning, simple put as the first line

    "use nodent-promise";  /* Note: Singular Promise */

The main issue you have is that your declaration of "tellYouLater" should NOT be declared async - it's a normal JS function that resolves it's Promise when done. Here's the corrected code via the on-line demo: http:https://nodent.mailed.me.uk/#function%20tellYouLater(sayWhat)%20%7B%20%20%0A%20%20%20%20return%20new%20Promise(function(%24return%2C%20%24error)%20%7B%0A%20%20%20%20%20%20%20%20setTimeout(function()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24return(sayWhat)%3B%0A%20%20%20%20%20%20%20%20%7D%2C%20500)%3B%0A%20%20%20%20%7D)%3B%0A%7D%0A%0Aasync%20function%20test()%20%7B%0A%20%20%20%20return%20await%20tellYouLater(%22Hi%20there%22)%3B%0A%7D%0A%0Aconsole.log(await%20test())%3B%0A

Both "use nodent-es7" and "use nodent-promise" work in the browser. For your use case (interfacing with standard JS functions that use callbacks) you'll probably want to "use nodent-promise" and include a Promise library (the online demo uses bluebird, but has been tested with native Chrome Promises, rsvp & when).

There are a few hoops to jump through using nodent in the browser, which I'm afraid I haven't documented very well. The best description can be found in issue #2. That having been said, the code has been tested on IE, FF and Chrome reasonably extensively in the, and once you've got the hang of it (specifically remembering to define Function.prototype.$asyncbind) it works well. Pre-compiling on the command line requires an extra build step - if you're using node.js to serve, you can get it to trans-compile on-the-fly which makes testing a but easier.

I hope that answers your question.

@mbrowne
Copy link
Author

mbrowne commented Apr 4, 2015

Thanks, it works now -- but only when using require('nodent')(). If I compile it (using nodent.js --out) and then try to run it directly with node, I get ReferenceError: $error is not defined.

I was using "use nodent-promises" (plural) at the top of the file before because that's what it says in the readme.

Thanks for the tips on running in the browser...it seems to me that the main advantage of your library over Babel is better performance (both server-side and especially client-side). It sounds like a promises library e.g. bluebird and the small $asyncbind function are the only dependencies to get it working in all modern browsers, is that right?

@MatAtBread
Copy link
Owner

You're quite right. $error is the top-level (i.e. unhandled) exception handler for await. You need to define it somewhere. When using nodent from inside node, it's defined as:

    global.$error = function(ex) { throw ex ; }

@MatAtBread MatAtBread reopened this Apr 4, 2015
@MatAtBread
Copy link
Owner

...in the browser obviously you need to using window rather than global, or define it once per file.

@mbrowne
Copy link
Author

mbrowne commented Apr 4, 2015

Thank you for the helpful info. That answers my questions, but for the sake of the next person it would be good to update the readme file so it says "use nodent-promise" instead of "use nodent-promises". Thanks :)

@MatAtBread
Copy link
Owner

Oops! Thanks for pointing that out. It's now corrected.

@mbrowne mbrowne closed this as completed Apr 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants