When my system is creating errors, those are collected and finally sent to a ticket API. All errors are grouped by their type of priority. So, if two different error types are thrown, two tickets should be sent.
Looks like:
var tickets = [{
type: 'validation',
component: 1,
time: '2016-04-13T10:26:41.420Z',
messages: [ [Object], [Object], [...] ]
}, {
type: 'fatal',
component: 1,
time: '2016-04-13T10:26:41.420Z',
messages: [ [Object] ]
}];
Every object within that array should be sent after another, delayed by 2 seconds. sendToTicketAPI() returns a Promise and I also want to return a promise when all tickets are sent. My thought was, I could resolve/reject it with .onEnd/.onError.
Code is:
var Bacon = require('baconjs'); //v0.7.71
createTicketWrapper(msg, fnResolve, fnReject) {
Bacon
.fromArray(tickets)
.bufferingThrottle(2000)
.flatMap(function (ticket) {
return sendToTicketAPI(ticket)
.then(function () {
return ticket;
});
})
.onError(function (err) {
console.error('Unexpected error msg', err);
if (fnReject)
fnReject();
})
.onEnd(function () {
if (fnResolve)
fnResolve()
});
}
But I'm getting:
[TypeError: Bacon.fromArray(...).bufferingThrottle(...).flatMap(...).onError(...).onEnd is not a function]
I don't understand why .onEnd is not a function!? Maybe another approach?
onError returns a unsubscribe function(which does not have an onEnd function: "Just like subscribe, this method returns a function for unsubscribing." via https://baconjs.github.io/api.html#observable-onerror), not an eventstream(which does).
try this:
var Bacon = require('baconjs'); //v0.7.71 createTicketWrapper(msg, fnResolve, fnReject) { var stream = Bacon .fromArray(tickets) .bufferingThrottle(2000) .flatMap(function (ticket) { return sendToTicketAPI(ticket) .then(function () { return ticket; }); }); stream.onError(function (err) { console.error('Unexpected error msg', err); if (fnReject) fnReject(); }) stream.onEnd(function () { if (fnResolve) fnResolve() }); }
Note that I assigned the eventstream created from flatMap to a variable, and then used that to subscribe to onError and onEnd seperately.