Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fanqingsong/lazyman_idea
to demo lazy man in async && chain style
https://github.com/fanqingsong/lazyman_idea
async chain lazyman promise settimeout
Last synced: 10 days ago
JSON representation
to demo lazy man in async && chain style
- Host: GitHub
- URL: https://github.com/fanqingsong/lazyman_idea
- Owner: fanqingsong
- Created: 2019-03-18T15:44:32.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-18T16:03:26.000Z (almost 6 years ago)
- Last Synced: 2024-11-14T18:54:25.513Z (2 months ago)
- Topics: async, chain, lazyman, promise, settimeout
- Language: JavaScript
- Homepage:
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lazyman algorithm idea demo
## this project is for demo lazyman idea
lazyman is meeted with chain style, and supports async operation in each chain logic#install and run
```
// see the promise version
npm run promise// to see the primitive version
npm run primitive
```# explain
usage code
Promise version
```function _LazyMan(name) {
this.promiseGetters = [];
var makePromise = function () {
var promiseObj = new Promise(function(resolve, reject){
console.log("Hi! This is " + name + "!");resolve();
})return promiseObj;
}this.promiseGetters.push(makePromise);
// 在各个Promise的then函数中,将任务序列穿起来
var self = this;
var sequence = Promise.resolve();
// Promise.resolve 等价于
// var sequence = new Promise(function (resolve, reject) {
// resolve();
// })
setTimeout(function(){
for (var i = 0; i < self.promiseGetters.length; i++) {
var nowPromiseGetter = self.promiseGetters[i];
var thenFunc = (function (nowPromiseGetter) {
return function () {
return nowPromiseGetter()
}
})(nowPromiseGetter);sequence = sequence.then(thenFunc);
};}, 0); // 在下一个事件循环启动任务
}_LazyMan.prototype.eat = function(name) {
var makePromise = function () {
var promiseObj = new Promise(function(resolve, reject){
console.log("Eat " + name + "~");resolve();
})return promiseObj;
}this.promiseGetters.push(makePromise);
return this; // 实现链式调用
}_LazyMan.prototype.sleep = function(time) {
var makePromise = function () {
var promiseObj = new Promise(function(resolve, reject){setTimeout(function(){
console.log("Wake up after " + time + "s!");
resolve();
}, time * 1000);
})return promiseObj;
}this.promiseGetters.push(makePromise);
return this;
}/* 封装 */
function LazyMan(name){
return new _LazyMan(name);
}
LazyMan("Hank").sleep(1).eat("dinner")
```