Matches responses with requests, expiring requests if they do not get a response in a timely manner.
Basic usage:
constm = newRequestResponseMatch(options); // Listen for when a response matches a request m.addEventListener(`match`, event=> { // event: { request:Req, response:Resp} }); // Or alternatively, listen for success and failures m.addEventListener(`completed`, event=> { // { request:Resp, response:Req|undefined, success:boolean } // 'response' will be data or a string error message }); m.request(req); // Note that some request was sent ... m.response(resp); // Call when a response is received
It's also possible to wait for specific replies:
// With a promise constresp = awaitm.requestAwait(req); // With a callback m.requestCallback(req, (success, resp) => { // Runs on success or failure })
It relies on creating an id of a request/response for them to be matched up. Use the key
option if the function can generate a key from either request or response. Or alternatively set both keyRequest and keyResponse for two functions that can generate a key for request and response respectively.
The easy case is if req & resp both have the same field:
constm = newRequestResponseMatch({ key: (reqOrResp) => { // Requests has an 'id' field // Response also has an 'id' field that corresponds to the request id returnreqOrResp.id; } });
A more complicated case:
constm = newRequestResponseMatch({ keyRequest: (req) => { // Requests have an 'id' field returnreq.id; }, keyResponse: (resp) => { // Responses have id under a different field returnresp.reply_to } })
By default, error will be thrown if a response is received that doesn't match up to any request.
Matches responses with requests, expiring requests if they do not get a response in a timely manner.
Basic usage:
It's also possible to wait for specific replies:
It relies on creating an id of a request/response for them to be matched up. Use the
key
option if the function can generate a key from either request or response. Or alternatively set bothkeyRequest
andkeyResponse
for two functions that can generate a key for request and response respectively.The easy case is if req & resp both have the same field:
A more complicated case:
By default, error will be thrown if a response is received that doesn't match up to any request.