Guest User

Untitled

a guest
Feb 17th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. //run this in node.js, firefox only, not in browser chrome for now
  2. const parallel = async (...items) => {
  3. const temp = [];
  4. for (const item of items) {
  5. temp.push(await item);
  6. }
  7. return temp;
  8. };
  9.  
  10. const someResult = async () => {
  11. return new Promise((resolve, reject) => {
  12. console.log('running someResult() ...');
  13. setTimeout(() => {
  14. console.log('someResult() is done');
  15. resolve('hello');
  16. }, 2000);
  17. });
  18. };
  19.  
  20. const anotherResult = async () => {
  21. return new Promise((resolve, reject) => {
  22. console.log('running anotherResult()...');
  23. setTimeout(() => {
  24. console.log('anotherResult() is done');
  25. resolve('world');
  26. }, 3000);
  27. });
  28. };
  29.  
  30. (async () => {
  31. console.log('running test case');
  32. const t0 = new Date()
  33. const [p1, p2] = await parallel(someResult(), anotherResult());
  34. const t1 = new Date()
  35. console.assert(p1 == 'hello')
  36. console.assert(p2 == 'world')
  37. console.assert(t1 - t0 < 3010)
  38. console.log(p1, p2, t1 - t0)
  39. console.log('all assertion passed, someResult(), anoterResult() is executed in parallel')
  40. })();
  41.  
  42. console.log('eof')
Add Comment
Please, Sign In to add comment