fun-mock
Version:
38 lines (35 loc) • 720 B
JavaScript
const LinkedList = require('friendly-linkedlist');
const linkedList = new LinkedList();
let executing = false;
module.exports = {
push(callback){
linkedList.addLast(callback);
},
exec(){
if (executing || linkedList.size() === 0) {
return;
}
executing = true;
const next = () => {
executing = false;
// 下一层循环检查
this.exec();
}
try{
const callback = linkedList.removeFirst();
if(callback){
const promise = callback();
if (promise) {
promise.then(() => {
next();
});
} else {
next();
}
}
} catch (e) {
console.error(e);
next();
}
},
};