javascript - $q deferred not firing continuations in Jasmine -
i creating unit tests work promises want resolve manually , finding promise continuations seem never hit. open console in jasmine debug panel, put reference $q
on window object...and
d = $q.defer() d.promise.then(x => console.log(`done`, x)) d.resolve(5)
nothing gets logged...
what going on?! $q
somehow working? not work every other deferred implementation in world works? reading (insanely meager) documentation wrong? how possible above promise resolved not have continuation fire?!
angularjs 1.5.8
logs "done 5"
in example
angular.module("app",[]) .run(function($q) { var d = $q.defer(); d.promise.then(x => console.log(`done`, x)); d.resolve(5); });
<script src="//unpkg.com/angular/angular.js"></script> <body ng-app="app"> <h1>promise example</h1> </body>
integration browser event loop
angularjs modifies normal javascript flow providing own event processing loop. splits javascript classical , angularjs execution context. use $apply()
enter angularjs execution context javascript. keep in mind in places (controllers, services) $apply
has been called directive handling event.
es6 promises handled javascript event loop. $q promises handled angularjs event loop. operations applied in angularjs execution context benefit angularjs data-binding, exception handling, property watching, etc.
in case of jasmine tests, use $rootscope.$apply()
enter angularjs execution context.
for more information, see angularjs developer guide - integration browser event loop
Comments
Post a Comment