Wednesday, 24 May 2017

understanding co library and javascript generator with example

I am trying to understanding co libary - https://github.com/tj/co and using this library with promises. I am using following code as example -

const co = require('co');

function foo(x) {
    return new Promise(function (resolve) {
        console.log('I am starting foo');
        const y = 8;
        console.log('I have passed y');
        const z = 16;
        resolve(x + y + z);
    });
}   

co(function* () {
    console.log('I Start');
    yield foo(5);
    console.log('I finish');
});

On running this code, 'I finish' never log to console.

I could not understand why it is not printing 'I finish' into console.

Thanks



via daljit

No comments:

Post a Comment