Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let works = [];
- function tick() {
- for (const work of works) {
- work();
- }
- }
- // add a work to the eventloop
- function enqueueWork(work) {
- works.push(work);
- }
- // remove a work form the eventloop
- function dequeueWork(w) {
- works = works.filter(o => o!==work);
- }
- function setTimeout(f, t) {
- const createdAt = Date.now();
- enqueueWork(function work() {
- if (createdAt + t < Date.now()) {
- dequeueWork(work);
- f();
- }
- });
- }
- function generateMessage(cb) {
- setTimeout(() => {
- cb('hello world!');
- }, 1000);
- }
- function msg() {
- generateMessage((msg) => {
- console.log('Message: ', msg);
- });
- }
- msg();
- while (true) {
- tick();
- }
Add Comment
Please, Sign In to add comment