Just face byte is jumpy, title very (~~~~~~) foundation, but answer badly namely. . . Video is hanged, hey, have good idea T.T
Be no good, my have to sticks the answer here, regrettablly interview official is not before me, otherwise my have to tells him, you look, this is solved still make do with at least
// Ti Mu Shi Shi Xian Xia Lie Lian Shi Diao Yong
const chain = new Chain()
chain
.eat()
.sleep(1000)
.eat()
.sleep(1000)
.work()
// Wo Yuan Lai Yong De promise Ba Suo You Fang Fa Du Bao Guo Qi Lai , Jian Zhi Chou Po Tian Ji ...
// Xia Mian Shi Xin De Jie Da
// Zhi Yu Li Mian You Xie Shu Xing Shi Bu Shi Yao Yong private Huo Zhe protected Shi Me De , Wo Jiu Lan De Xi Jiu Liao
interface EatAction {
name: \"eat\";
params: [];
}
interface SleepAction {
name: \"sleep\";
params: [number];
}
interface WorkAction {
name: \"work\";
params: [];
}
type Action = EatAction | SleepAction | WorkAction
class Chain {
running = false
actions: Action[] = []
trigger() {
if (this.running) { return }
const action = this.actions.shift()
if (!action) { return }
this.running = true
switch (action.name) {
case \"eat\":
console.log(\"eat\")
this.running = false
this.trigger()
break
case \"work\":
console.log(\"work\")
this.running = false
this.trigger()
break
case \"sleep\":
setTimeout(() => {
console.log(\"sleep\")
this.running = false
this.trigger()
}, action.params[0])
break
default:
break
}
}
eat() {
this.actions.push({
name: \"eat\",
params: [],
})
this.trigger()
return this
}
sleep(n: number) {
this.actions.push({
name: \"sleep\",
params: [n],
})
this.trigger()
return this
}
work() {
this.actions.push({
name: \"work\",
params: [],
})
this.trigger()
return this
}
}
It is a bit simpler so
“`tsClass Chain {Private Actions: Function[] = [];Constructor() {SetTimeout(async () =>{For (const Action Of This.actions) {Await Action.call(this);
}
});
}
Eat() {This.actions.push(() =>Console.log(‘eat’));
Return This;
}
Sleep(timeout: Number) {This.actions.push(() =>{Console.log(‘sleep ‘ , timeout);Return New Promise((resolve) =>SetTimeout(() =>{Resolve();
} , timeout)
) ;
});Return This;
}Work() {This.actions.push(() =>Console.log(‘work’));
Return This;
}
}
Const Chain = New Chain();
Chain.eat().sleep(1000).eat().sleep(1000).work();
“`
@jason94 you so write also had held out, if have,just add the incident that go in after Constructor namely, won’t carry out, be like: SetTimeout(() =>{Chain.eat() } , 5000)
“`tsClass Chain{Constructor(promise){If(promise){This. _promise=promise;
}else{This. _promise=Promise.resolve();
}
}Eat(){Return This. _next(()=>{console.log(‘eat’)});
}Work(){Return This. _next(()=>{console.log(‘work’)});
}Sleep(ms){Return This. _next(async ()=>{Await New Promise(resolve=>setTimeout(resolve, ms) )
});
}
_next(someAction){Const NextPromise=(async ()=>{Await This. _promise;Await SomeAction();
})()Return New Chain(nextPromise);
}
}
“`
My train of thought, did not check, the likelihood has Bug
Class Chain{Task=Promise.resolve()Eat() {This.task=this.task.then(_=>console.log(‘eat’) )Return This
}Work() {This.task=this.task.then(_=>console.log(‘work’) )Return This
}Sleep(t) {This.task=this.task.then(_=>new Promise(r=>{Console.log(‘sleep’ , t)SetTimeout(r, t)
}) )Return This
}
}
Promise is written very simple
@mxT52CRuqR6o5 @treemonster takes aim probably, should be problem of it doesn’t matter. What my train of thought at that time follows 4 buildings is same, can be those who write is much uglier. . .