Ivan Asmer 6 years ago
parent
commit
f3c61f016f
2 changed files with 25 additions and 2 deletions
  1. 24 1
      README.md
  2. 1 1
      public/index.js

+ 24 - 1
README.md

@@ -1,3 +1,26 @@
 # Async WS:
 
-node js, web socket, promises and generator sample
+Almost mirror sample of:
+
+Catching data from `Stream` or `WebSocket` and pass it as generated 
+sequence of `Promise`s, which can be handled by pseudo synchronous 
+`for ... of` or `for await ... of`
+
+For solve this `Promise` with __outside control__ used. It needed
+because data passed many times to stream ondata/onmessage/onchunk/onfuckingsomethingelse,
+not only once as in `Promise` callback. 
+
+So, promises should be resolved from chunk event outside of 
+closure, where resolve and reject accessible.
+
+When chunk becomes, it saved into chunks queue, and if promise
+for that chunk are already yielded, it resolves
+
+When promise yielding outside of generator, if chunk for this promise
+already saved in queue, promise resolves with data of that chunk
+
+So, generated promises gives ability to handle stream in imperative
+manner if `for of` loop with `await`.
+
+Than, recieved messages passed as JSON with some RPC function call 
+with params

+ 1 - 1
public/index.js

@@ -85,7 +85,7 @@ let RPC = {
 (async () => {
     while (true){
         try{
-            socket = new WebSocket("ws://localhost:8999/");
+            socket = new WebSocket(`ws://${location.host}/`);
             let aGena  = asynchronize({s: socket, chunkEventName: 'message', endEventName: 'close'});
             for await (let msg of aGena()){
                 let data = JSON.parse(msg.data)