co-test2.js 429 B

1234567891011121314151617181920212223242526
  1. /**
  2. * Created by fy on 15-10-1.
  3. */
  4. 'use strict';
  5. var co = require('co')
  6. , thunkify = require('thunkify');
  7. function testFunc(p1, p2, cb) {
  8. setTimeout(function () {
  9. clearInterval(itr);
  10. }, 300);
  11. var itr = setInterval(function () {
  12. cb(null, p1 + p2);
  13. }, 80);
  14. }
  15. var _testFunc = thunkify(testFunc);
  16. co(function *() {
  17. var r1 = yield _testFunc(3, 2);
  18. return r1;
  19. }).then(function (result) {
  20. console.log(result);
  21. });