1
\$\begingroup\$

I'm experimenting with reactive JS by writing mocha tests. I wrote a test that makes a certain number of requests to an API before stopping. However, this being my first experience with RxJS, I don't know if I'm doing things in the best way. Are there any obvious mistakes I'm making here?

   it('uses canary', function(done) {
  var eventEmitter = new EventEmitter();
  var iterations = 3;
  var source = Rx.Observable.fromEvent(eventEmitter, 'data').take(iterations);

  var subscription = source
     .subscribe(
        function(data) {
           console.log(data);
        },
        function(err) {
           if (err) {
              throw err;
           }
        },
        function() {
           console.log('completed');
           done();
        }
     );

  for (var k = 0; k < iterations; k++) {
     request(app)
        .get('/api/canarycanary')
        .set('Accept', 'application/json')
        .expect('Content-Type', /json/)
        .end(function(err, res) {
           eventEmitter.emit('data', res.body);
        });
  }

});

\$\endgroup\$
  • \$\begingroup\$ What is the use for making k iterations? \$\endgroup\$ – Attilio Oct 5 '16 at 20:13
  • \$\begingroup\$ long story short, it's much easier for me to use K than i \$\endgroup\$ – PunDefeated Oct 5 '16 at 22:16
  • \$\begingroup\$ That's not the point of my question :) I mean, why do you make the same operation k times, and not just once? \$\endgroup\$ – Attilio Oct 6 '16 at 17:58
  • \$\begingroup\$ Oh, my misunderstanding. I want to make sure the API can handle multiple asynchronous requests, which is why I am doing the operation K times. Make sense? \$\endgroup\$ – PunDefeated Oct 6 '16 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.