Thursday, 25 May 2017

How will coroutine behaves when the promise is resolved multiple times and co-routine have next yield

For promise & co-routine, I've used bluebird npm package in NodeJs.

Can someone please help in understanding the behavior of below code when promise is getting resolved multiple times.

Question:

  1. What will happens to coroutine when the promise resolved for multiple times?

  2. Will the second yield will be affected by the multiple yield of first.

    const bluebird = require("bluebird");

    function func1() {
        return new bluebird((resolve, reject) => {
            let c = 0;
            SetInterval(() => {
                c++;
                let cc = c;
                console.log(`c=${c}`);
                resolve(true);
            }, 1000);
        });
    }
    
    let run1 = bluebird.coroutine(function*() {
        try {
            yield func1();
            yield func1();
            yield func1();
            yield func1();
        } catch (e) {
            console.dir(e);
        }
    });
    
    


via dearvivekkumar

No comments:

Post a Comment