4

I am trying to do a function like async/await in C# but in node js I have found an example but It gives me an error.

here is the code

function* gotNews(response){    
        console.log("in gotNews");
        str='';
        response.on('data', function (chunk) {
            str += chunk;
        });    
        response.on('end', function () {
              str = JSON.parse(str);
              console.log(str);
              fetchCategories();
        });
        return str;
}


function fetchNews(sourceURL){
        console.log("in fetch news");
             sourceURL = url.parse(sourceURL);
             console.log(sourceURL);
        var options = 
        {
            host: sourceURL.host,
            port: 134,
            path: sourceURL.path,

            method: 'GET',
        };
        var req = http.request(options,yield gotNews);//start request and recive response in gotSources
        req.end();
}

I am using the * yield operation but gives me an error

ErrorC:\Users\Alaa\Desktop\Fluid_layout_with_jQuery_Masonry\1\app.js:198
        var elnewselygat = yield gotNews();
                                 ^^^^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
7
  • is it something special with function* (I mean STAR)? I never saw function declared like that Commented Nov 20, 2013 at 11:44
  • @tomekK The * in function* is your async keyword. yield is your await keyword. Commented Nov 20, 2013 at 11:45
  • 2
    As I said there is no such a thing native in JavaScript. The example which you show requires a custom library github.com/bjouhier/galaxy and I guess that it reads your functions and does some magic to remove the * sign. Commented Nov 20, 2013 at 11:53
  • 1
    Have you tried promises? github.com/cujojs/when I know its not the same, but as far as I know, it helps you solve the same issue in a different way Commented Nov 20, 2013 at 11:57
  • 3
    Did you run node with the --harmony parameter? You'll also need node >= 0.11.2. Commented Nov 20, 2013 at 12:03

3 Answers 3

2

You'll need generators and promises to make it as easy as async/await/Task.

As @Paul stated, generators are a "future feature" so you need to pass --harmony or --harmony-generators. Also, generators are supported in V8 3.19, which is only in Node.js 0.11.2 or newer.

The JavaScript community is currently considering a number of possible approaches; there's a good overview of several of them here, and there are other libraries out there as well.

Sign up to request clarification or add additional context in comments.

Comments

0

check: https://github.com/luciotato/waitfor-es6

It does what you want, with generators. (no promises required)

function* ----> c# async

yield wait.for(fn...) ---> c# await

Comments

0

You can use async/await with babel --stage 1 --optional runtime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.