use()
respond()
check()
wait()
join()
reject()
next()
timeout()
wait()
operatorIn situations that you want to conduct some (potentially asynchronous) task based on
incoming requests, and it is important to wait for this task to finish before
moving down the observable sequence, you can use wait()
operator:
1import { wait, validate, respond } from 'rxxpress';
2
3
4const emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
5
6
7router.put('/users/update/email')
8.pipe(
9 authenticate(), // --> authenticate the user
10 validate(({req}) => emailRegex.test(req.body)), // --> validate the new email address
11 wait(async ({req}) => { // --> save the new email address
12 await users.update(req._.user.id, { // --> save the new email address
13 email: req.body // --> save the new email address
14 }) // --> save the new email address
15 }), // --> save the new email address
16 respond(() => 'Updated!') // --> respond
17)
18.subscribe();
info NOTICE
If you do not need to wait for the task before moving along, it is more efficient to use RxJs's tap operator instead.
Similar to check()
and respond()
, you can provide synchronous functions or functions
returning observables. In case your wait function returns an observable, the wait()
operator
will wait for the first value emitted by that observable:
1router.post('/')
2.pipe(
3 // ...
4 wait(({req}) => ajax({ // --> trigger user's webhook
5 method: 'POST', // --> trigger user's webhook
6 url: req._.user.webhook, // --> trigger user's webhook
7 body: ... // --> trigger user's webhook
8 })), // --> trigger user's webhook
9 respond(() => 'Webhook Triggered!')
10)
wait()
operator waits until its task is finished and then passed incoming
packets down the observable sequence.