generator-wrap

Wrapper for generators to transform them in coroutines.

Usage no npm install needed!

<script type="module">
  import generatorWrap from 'https://cdn.skypack.dev/generator-wrap';
</script>

README

generator-wrap Build Status

Wrapper for generators to transform them in coroutines.

Installation

$ npm install generator-wrap

Example

var wrap = require('generator-wrap');

function someAsyncOperation() {
    return new Promise(function(resolve, reject){
        setTimeout(function(){
            resolve(3.1415);
        }, 1000);
    });
}

wrap(function*(){
    console.log('Hello World from a coroutine!');
    var asyncObtainedValue = yield someAsyncOperation();
    console.log('Async Value: '+asyncObtainedValue);
})();

API

wrap(generatorFunction)

Wraps the generator in a function that is returned. When the function is executed, yielded values are treated as promises and their resolved values or their rejected errors are passed back in the generator.

License

MIT