Bluebird Tap (*spy, intercept the promise success chain without interfering)

JS
S
JavaScript

Bluebird provides tap as mechanism to intercept the promise success chain without affecting data being passed through the chain. http://bluebirdjs.com/docs/api/tap.html

1// Function Flow
2function ops(body) {
3  service.asyncOp(body)
4    .then(function provision(operation) {
5      const samples = db.getData({ q: 'samples' });
6      const telemetry = db.getData({ q: 'telemetry' });
7      const service = db.updateService({ q: 'dates', body: operation });
8      return [samples, telemetry, service];
9    })
10    .spread(function updateCache(samples, telemetry, service) {
11      cache.storeData(samples);
12      cache.storeData(telemetry);
13      const notification = ws.prepareNotification(service);
14      return notification;
15    })
16    // Interceptor function - does not interfere with bluebird's spread chain
17    .tap(function doStuffFreely(notification){
18       logger.log(notification);
19    })
20    // End of chain
21    .then(function finalizeRequest(notification) {
22      res.send(200);
23    })
24    .catch(function handleError(err) {
25      logger.log(err, 'Error while processing the transaction');
26      res.send({
27        code: err.code,
28        message: err.message,
29        metadata: err.metadata
30      });
31    });
32}
33
34// Caller
35ops();

Created on 3/24/2018