EVT
HomeGitHub
v1
v1
  • Why EVT ?
  • Overview
  • API Documentation
    • Evt<T>
      • evt.attach*(...)
      • evt.post*(data)
      • evt.waitFor(...)
      • evt.evt[Attach|Detach]
      • evt.pipe(...)
      • evt.getHandlers()
      • evt.isHandled(data)
      • evt.detach(ctx?)
      • evt.enableTrace(...)
      • evt.setMaxHandlers(n)
      • toStateful(initialState)
      • evt.getStatelessOp(op)
      • Evt.create(initalState?)
      • Evt.newCtx<T>()
      • Evt.getCtx(object)
      • Evt.from<T>(...)
      • Evt.merge([ evt1, evt2, ... ])
      • Evt.loosenType(evt)
      • Evt.factorize(evt)
      • Evt.asPostable(evt)
      • Evt.asNonPostable(evt)
      • Evt.setDefaultMaxHandlers(n)
    • Ctx<T>
    • Operator<T, U> (type)
    • StatefulEvt<T>
    • Helper types
    • Handler<T, U> (type)
  • React hooks
  • Extending Evt
Powered by GitBook
On this page
  • Without timeout
  • With timeout
  • Difference between evt.waitFor(...) and evt.attachOnce(...)
  1. API Documentation
  2. Evt<T>

evt.waitFor(...)

Previousevt.post*(data)Nextevt.evt[Attach|Detach]

Last updated 2 years ago

Method that returns a promise that will resolve when the next matched event is posted.

waitFor is essentially evt.attachOnce(...) but you don’t provide a callback. It accepts the same arguments and return the same promise.

Essentialy the same but , there is a key difference between a handler attached via waitFor and a handler attached with attach* as explained below.

Without timeout

By default the promise returned by waitFor will never reject.

import { Evt } from "evt";

const evtText = Evt.create<string>();

setTimeout(()=> evtText.post("Hi!"), 1500);

(async ()=>{

    //waitFor return a promise that will resolve next time 
    //post() is invoked on evtText.
    const text = await evtText.waitFor();

    console.log(text);

})();

With timeout

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, EvtError } from "evt";

const evtText = Evt.create<string>();

(async ()=>{

    try{

        const text = await evtText.waitFor(500);

        console.log(text);

    }catch(error){

        console.assert(error instanceof EvtError.Timeout);
        //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 1000
const timeout= ~~(Math.random() * 1000);

//There is a fifty-fifty chance "Hi!" is printed else it will be "TIMEOUT!".
setTimeout(
    ()=> evtText.post("Hi!"), 
    timeout
);

Difference between evt.waitFor(...) and evt.attachOnce(...)

const pr= evt.waitFor() is NOT equivalent to const pr= evt.attachOnce(()=>{})

evt.waitFor() is designed in a way that makes it safe to use async procedures.

Basically it means that the following example prints A B on the console instead of waiting forever for the secondLetter.

import { Evt } from "evt";

const evtText = Evt.create<string>();

(async ()=>{

    const firstLetter = await evtText.waitFor();
    const secondLetter = await evtText.waitFor();

    console.log(`${firstLetter} ${secondLetter}`);

})();

evtText.post("A");
evtText.post("B");

//"A B" is printed to the console.

Run this if you want to understand how this behavior prevent from some hard to figure out bugs.

not exactly the same
Run the example
Run the example
more practical example