Skip to content

Commit

Permalink
mainshi
Browse files Browse the repository at this point in the history
  • Loading branch information
mogu authored and mogu committed Jul 31, 2020
1 parent 39af837 commit 049eb7a
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions 面试题/头条.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
class Manager {
constructor(max) {
this.max = max;
}
add(fn) { // fn: () => Promise<any>
}
}
const wrapper = (str, time) => {
return () => {
return new Promise(res => {
setTimeout(() => {
console.log(str);
res();
}, time)
})
}
}
const m = new Manager(2);
m.add(wrapper('a', 1000));
m.add(wrapper('b', 500));
m.add(wrapper('c', 300));
0 start
500 b
800 c
1000 a
*/


class Manager {
constructor(max) {
this.max = max
this.cbs = []
this.queen = []
}
add(fn) {
if(this.cbs.length < this.max) {
this.cbs.push(fn)
fn().then(res => {
let index = this.cbs.indexOf(fn)
this.cbs.splice(index, 1)
if(this.queen.length > 0) {
this.add(this.queen.shift())
}
})
} else {
this.queen.push(fn)
}
}
}

const wrapper = (str, time) => {
return () => {
return new Promise(res => {
setTimeout(() => {
console.log(str);

res();
}, time)
})

}
}

const m = new Manager(2);

m.add(wrapper('a', 1000));
m.add(wrapper('b', 500));
m.add(wrapper('c', 300));

0 comments on commit 049eb7a

Please sign in to comment.