Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The co's then-function called before than thunkify function #305

Open
lhlPersonal opened this issue Nov 20, 2016 · 2 comments
Open

The co's then-function called before than thunkify function #305

lhlPersonal opened this issue Nov 20, 2016 · 2 comments

Comments

@lhlPersonal
Copy link

let co = require('co');
let Thunk = require('thunkify');

function testThunk(arg1, arg2, callback) {
console.log(arg1, arg2);
setTimeout(()=> {
callback('', 'thunk test');
}, 200);
}

co(function *() {
return Thunk(testThunk)('arg1', 'arg2')((err, data)=> { //return a thunkify function.
console.log('thunk call result:', data);
});
}).then(result=> {//The then-function called before than thunkify function,is that right?
console.log('co result:', result);
}
);

@ifsnow
Copy link

ifsnow commented Nov 22, 2016

There's something wrong with your source.

  • You should use yield in front of thunk.
  • testThunk's callback is called by co (systematically)
  • You should return thunk's result to use this in co's then.

Modified source below.

let co = require('co');
let Thunk = require('thunkify');

function testThunk(arg1, arg2, callback) {
  console.log(arg1, arg2);
  setTimeout(()=> {
    callback(null, 'thunk test');
  }, 200);
}

co(function *() {
  var result = yield Thunk(testThunk)('arg1', 'arg2');
  console.log(`testThunk's result : ${result}`);
  return result;
}).then(result => {//The then-function called before than thunkify function,is that right?
  console.log('co result:', result);
});

@lhlPersonal
Copy link
Author

OK,I see.Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants