javascript - What are the benefits of using the co library with promises instead of with thunks? -
so i've been reading usage of co library, , general design pattern i've seen in blog posts wrapping functions have callbacks in thunks. using es6 generator yield thunks co object. this:
co(function *(){ var = yield read(‘readme.md’); var b = yield read(‘package.json’); console.log(a); console.log(b); }); function read(path) { return function(done){ fs.readfile(path, ‘utf8', done); } } and can understand because brings benefits of promises better readability , better error handling.
but what's point of using co if have promises available?
co(function* () { var res = yield [ promise.resolve(1), promise.resolve(2), promise.resolve(3), ]; console.log(res); // => [1, 2, 3] }).catch(onerror); why not like
promise.all([ promise.resolve(1), promise.resolve(2), promise.resolve(3), ]).then((res) => console.log(res)); // => [1, 2, 3] }).catch(onerror); to me, co makes code more confusing compared promise version.
no real cases, no. unless hate promise constructor (in case, bluebird promisify rescue).
when have promises natively, valid usecases callbacks called once single value moot.
Comments
Post a Comment