Method that returns a promise that will resolve when the next matched event is posted.
Without timeout
By default the promise returned by waitFor will never reject.
import { Evt } from"evt";constevtText=Evt.create<string>();setTimeout(()=>evtText.post("Hi!"),1500);(async ()=>{//waitFor return a promise that will resolve next time //post() is invoked on evtText.consttext=awaitevtText.waitFor();console.log(text);})();
As with attach*, it is possible to set what is the maximum amount of time we are willing to wait for the event before the promise rejects.
import { Evt, TimeoutEvtError } from"evt";constevtText=Evt.create<string>();(async ()=>{try{consttext=awaitevtText.waitFor(500);console.log(text); }catch(error){console.assert(error instanceofTimeoutEvtError);//Error can be of two type:// -EvtError.Timeout if the timeout delay was reached.// -EvtError.Detached if the handler was detached before // the promise returned by waitFor have resolved. console.log("TIMEOUT!"); }})();//A random integer between 0 and 1000consttimeout=~~(Math.random() *1000);//There is a fifty-fifty chance "Hi!" is printed else it will be "TIMEOUT!".setTimeout( ()=>evtText.post("Hi!"), timeout);
import { Evt } from"evt";constevtText=Evt.create<string>();(async ()=>{//const firstLetter = await new Promise(resolve=> evtText.attachOnce(resolve));constfirstLetter=awaitevtText.waitFor();//const secondLetter = await new Promise(resolve=> evtText.attachOnce(resolve));constsecondLetter=awaitevtText.waitFor();console.log(`${firstLetter}${secondLetter}`);})();evtText.post("A");evtText.post("B");//"A B" is printed to the console. // Now, if you comment out the implementation using .attachOnce you'll see that// the second letter is lost, we never reach the console.log