Created
September 7, 2018 21:30
-
-
Save alexeyraspopov/658dd6f383121b6ebf26f67cee7a1bfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import Coroutine from 'react-coroutine'; | |
import Deferred from './Deferred.js'; | |
export default Coroutine.create(Counter); | |
async function* Counter() { | |
let counter = 0; | |
while (true) { | |
let event = new Deferred(); | |
yield ( | |
<section> | |
<p>{counter}</p> | |
<button onClick={() => event.resolve()}>Increment</button> | |
</section> | |
); | |
await event.isResolved(); | |
counter++; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class Deferred { | |
constructor() { | |
this.resolve = null; | |
this.promise = new Promise(resolve => { | |
this.resolve = resolve; | |
}); | |
} | |
resolve(value) { | |
this.resolve(value); | |
} | |
isResolved() { | |
return this.promise; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment