javascript - What are the benefits of using the co library with promises instead of with thunks? - Stack Overflow

So I've been reading about the usage of the co library, and the general design pattern I've s

So I've been reading about the usage of the co library, and the general design pattern I've seen in most blog posts is wrapping functions that have callbacks in thunks. Then using an es6 generator to yield those thunks to the co object. Like this:

co(function *(){
  var a = 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 that I can understand because it brings all the benefits of promises like better readability and better error handling.

But what's the point of using co if you already 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 something 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 the code look more confusing pared to the Promise version.

So I've been reading about the usage of the co library, and the general design pattern I've seen in most blog posts is wrapping functions that have callbacks in thunks. Then using an es6 generator to yield those thunks to the co object. Like this:

co(function *(){
  var a = 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 that I can understand because it brings all the benefits of promises like better readability and better error handling.

But what's the point of using co if you already 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 something 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 the code look more confusing pared to the Promise version.

Share Improve this question edited Mar 31, 2016 at 20:57 Filip Dupanović 33.7k14 gold badges89 silver badges119 bronze badges asked Jan 5, 2016 at 21:54 m0menim0meni 16.5k18 gold badges87 silver badges148 bronze badges 4
  • 2 I haven't found any cases (ignoring es7) where it makes sense to me to use generators with promises. Most of the examples i've seen just make it more plicated just so that they can use generators. – Kevin B Commented Jan 5, 2016 at 21:57
  • co has it's uses, doesn't mean you use it everywhere even if it's not appropriate! – Jaromanda X Commented Jan 5, 2016 at 21:59
  • @KevinB for the es7 features are you referring to async/await or is there something else? – m0meni Commented Jan 5, 2016 at 21:59
  • 2 referring to async/await, it does allow for some cool things. – Kevin B Commented Jan 5, 2016 at 22:00
Add a ment  | 

1 Answer 1

Reset to default 8

No real cases, no. Unless you really hate the promise constructor (in which case, bluebird promisify to the rescue).

When you have Promises natively, nearly all valid usecases for callbacks that are called once with a single value are effectively moot.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744170193a4561498.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信