1
ericls 2020-02-15 01:23:07 +08:00 via iPhone
包个 promise 就行啦
|
2
godoway 2020-02-15 02:07:12 +08:00
听说过 rxjs 吗
var subject = new Subject() subject.subscribe(msg => console.log(msg)) subject.next(1) subject.next(2) subject.next(3) |
3
jinliming2 2020-02-15 02:40:23 +08:00 via iPhone
用 async generator 比较合适?
|
4
noe132 2020-02-15 02:47:58 +08:00 via Android
你听说过 eventemitter 吗
|
5
xcstream OP 我知道你们说的东西,但是 只在这 3 个地方填代码是否可以实现。
|
6
chnwillliu 2020-03-11 09:36:51 +08:00
```
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); } } ``` |
7
xcstream OP @chnwillliu 试了一下可以的!
|