This article describes the method of using yield to simulate multithreading in JavaScript. Share it for your reference. The specific analysis is as follows:
There are yield methods in python and C#. Through yield, many functions that can only be implemented by multiple threads.
There are version requirements for javascript: JavaScript 1.7
function Thread( name ) { for ( var i = 0; i < 5; i++ ) { Print(name+': '+i); yield; } } //// thread management var threads = []; // thread creation ( new Thread('foo') ); ( new Thread('bar') ); // scheduler while () { var thread = (); try { (); (thread); } catch(ex if ex instanceof StopIteration) {} }
The above code input result is as follows:
foo: 0 bar: 0 foo: 1 bar: 1 foo: 2 bar: 2 foo: 3 bar: 3 foo: 4 bar: 4
I hope this article will be helpful to everyone's JavaScript programming.