Does node.js support yield?

后端 未结 10 674
误落风尘
误落风尘 2021-01-31 01:51

Is there any way to get generators into node.js?

I\'m currently faking them with callbacks, but I have to remember to check the response of the callback inside of my gen

10条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 02:35

    Yes and no.

    var myGen = (function () {
        var i = 0;
        return function () {
            i++; return i; }
    })();
    var i;
    while ((i = myGen()) < 100 ) {
        do something; }
    

    As you see, you can implement something like one using closures, but it does not have native generators.

提交回复
热议问题