https://github.com/skyhacker2/yieldflow
https://github.com/skyhacker2/yieldflow
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/skyhacker2/yieldflow
- Owner: skyhacker2
- Created: 2016-03-04T01:42:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-03-04T02:12:43.000Z (over 10 years ago)
- Last Synced: 2025-02-28T18:57:57.755Z (over 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# yieldflow
> Asynchronous becomes synchronous with the yield
## Install
```
npm install yieldflow
```
## Usage
```
var yieldflow = require('yieldflow');
var assert = require('assert');
function login(username, password, callback) {
assert(username);
assert(password);
assert(callback);
setTimeout(()=>callback(0), 1000);
}
function sendmsg(msg, callback) {
assert(msg);
setTimeout(()=>callback(msg), 500);
}
yieldflow(function* (next){
var code = yield login('user', '123', (code)=>{
next(code);
});
console.log('login: ' + code);
var sendText = yield sendmsg('xxx', (text)=>{
next(text);
});
console.log('send: ' + sendText);
sendText = yield sendmsg('fuck', (text)=> {
next(text);
});
console.log('send: ' + sendText);
});
```
result print:
```
login: 0
send: xxx
send: fuck
```
`login` and `sendmsg` is two asyns function.
`yeildflow` pass a next function to you, you will call `next(result)` in the async function callback to return the result to yield operation (`var code = yield login(...);`) and move one step.
Have fun.
## License
MIT: http://rem.mit-license.org