javascript - Calling a generator function from a client that performs asynchronous calls in a loop using Promises -


i need crawl entire dom top bottom processing each node. when node meets criteria, need process information node synchronously, meaning don't continue on next node in dom until processing work has completed.

the processing of node involves multiple ajax calls , carry out these calls asynchronously. after ajax calls have completed, can final processing work done , result returned point in dom crawling.

the solution leaning towards create generator function crawling of dom , executes yield statement when node criteria has been met , contents of node transferred client (via yield statement) called next function.

the client initiated call generator function parse node's data, create multiple promises , execute these promises asynchronously promise.all().

once promises have completed , processing has finished, generator function called again time processed data client returned yield statement left off merge dom.

what isn't clear me how create client code performs node processing includes executing promises. if possible, avoid recursively calling client function. here pseudo code can imagine being done:

generator:

function *crawldom() {   let node = "html";    function processnode(node) {     if (node.value == "somecriteria") {       let processedvalue = yield node.value;       // processed value...     }      (let = 0; < node.children.length; i++) {       processnode(node.children[i]);     }   } } 

node processing client:

function processnodedata() {     var crawler = crawldom();     var processeddata = "";      while (true) {        var nodedata = crawler.next(processeddata).value;        var items[] = nodedata.split("\n");        var promises[] = [items.length];         (let = 0; < items.length; i++) {           promises[i] = new promise(function (resolve, reject) {              // work              resolve(result);           }        }         promises.all(promises)          .then(function(result) {             processeddata = "some new data";             // final processing.             // @ point, need call generator function again processed data.             // wrong place because "while" loop have moved on             // next node while "then" code being executed.          })      } } 

how resolve looping issue next call generator function occurs after promise.all() has completed?

the key executing promises in loop place promises inside async function:

function processnode(x) {     return new promise(resolve => {         settimeout(() => {             resolve(x);         }, 2000);     }); }  async function startnodeprocessing() {     while(true) {         var = processnode(20);         var b = processnode(30);         var r = await + await b;     }      return; } startnodeprocessing(); 

the line of code:

var r = await + await b; 

blocks until promises have completed. means don't need promise.all, although still use that. after blocking has completed, can continue repeating loop. there no recursive calling , blocking native async function meaning not blocking main thread, won't see ui issues if have lengthy code in loop process.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -