javascript - Call intern asyn callback more than once -
is possible specify circumstances asyn callback should called in intern? let's have test testing method performs several xhr requests in row. specificaly, i'm trying test implementation of callback provided strophe.connect. works way sends several xhr requests server, handle initial xmpp (bosh) handshake.
is possible test in 5 seconds callback called status code 5 (connected)? problem callback triggered more once , need wait right status code, throw away others.
var dfd = this.async(5000); conn.connection.connect("jid", "pass", dfd.callback(function(status) { if(status === 5) { expect(status).to.have(something); } else { // here, need intern // nothing, let callback // triggered once more } return true; }));
because callback called more once, i'd suggest explicitly resolving deferred rather wrapping callback dfd.callback. try this:
var dfd = this.async(5000); conn.connection.connect('jid', 'pass', dfd.rejectonerror(function (status) { if (status !== 5) { return; } // assertions here // explicitly resolve test since successful point dfd.resolve(); })); first, you'll notice wrap callback dfd.rejectonerror. convenience method reject deferred if error thrown during execution of callback function. next, got rid of dfd.callback, attempt resolve deferred if wrapped callback executes without throwing error. because callback called multiple times, won't work. explicitly resolving deferred when criteria met (status !== 5 , assertions pass) callback can execute number of times long deferred resolves within 5 seconds.
Comments
Post a Comment