Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ee = new (require("events")).EventEmitter();
  2. const array = Array(1000).fill(0);
  3. const delay = ms => new Promise(r => setTimeout(r, ms));
  4.  
  5. function funzioneAsincrona() {
  6.     return new Promise(async (resolve, reject) => {
  7.         ee.on("mioEvento", reject);
  8.         for (let i = 0; i < array.length; i++) {
  9.             // simula XHR
  10.             await delay(100);
  11.             console.log("op", i);
  12.         }
  13.     });
  14. }
  15. setTimeout(() => ee.emit("mioEvento"), 2000);
  16.  
  17. (async function run() {
  18.     try {
  19.         await funzioneAsincrona();
  20.     } catch (err) {
  21.         console.log("<<<<<< error");
  22.     }
  23. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement