This topic created in 2300 days ago, the information mentioned may be changed or developed.
class JSChannel{
constructor(){
//todo
}
output(){
//todo
}
input(item){
//todo
}
}
var chan = new JSChannel()
chan.input(1)
chan.input(2)
chan.input(3)
async function runloop(){
while(1){
var v = await chan.output()
console.log(v)
}
}
runloop()
setInterval(
function(){
chan.input(new Date())
},1000
)
//一端输入一端输入,没有输入时候停住,知道有输入时返回
//输出 1 2 3 然后一秒出一次时间
7 replies • 2020-03-11 11:20:54 +08:00
 |
|
1
ericls Feb 15, 2020 via iPhone
包个 promise 就行啦
|
 |
|
2
godoway Feb 15, 2020
听说过 rxjs 吗 var subject = new Subject() subject.subscribe(msg => console.log(msg))
subject.next(1) subject.next(2) subject.next(3)
|
 |
|
3
jinliming2 Feb 15, 2020 via iPhone
用 async generator 比较合适?
|
 |
|
4
noe132 Feb 15, 2020 via Android
你听说过 eventemitter 吗
|
 |
|
5
xcstream Feb 17, 2020
我知道你们说的东西,但是 只在这 3 个地方填代码是否可以实现。
|
 |
|
6
chnwillliu Mar 11, 2020
``` class JSChannel { constructor() { this.outputBuffer = []; this.inputBuffer = []; }
output() { if(this.inputBuffer.length) { const input = this.inputBuffer.shift(); return Promise.resolve(input); }
const defferred = (() => { let resolve, reject; let promise = new Promise((res, rej) => { resolve = res; reject = rej; });
return { promise, resolve, reject }; })();
this.outputBuffer.push(defferred); return defferred.promise; }
input(item) { if(this.outputBuffer.length) { const output = this.outputBuffer.shift(); output.resolve(item); return; }
this.inputBuffer.push(item); } } ```
|