Why do you execute $ q.when () without passing a promise / value to it?

According to Angular docs in $ q , $ q.when () is expecting a promise / value to be passed.

But I just came across another code where it called without passing any parameters. Here is a simplified version of what I see:

var modal = false; if (modalOpen) { return $q.when() } modalOpen = true; modal = newModal({ template: opts.template, }); modal.result.finally(function(){ modalOpen = false; }); } 
+5
source share
1 answer

Methods must either return synchronously or return asynchronously to remain consistent. If the method sometimes returns synchronously and still wants to save the fact, then it is already resolved transparently - it returns an empty resolved promise. Having an API that sometimes returns promises, and sometimes synchronously, is a recipe for problems .

Using $q.when is the easiest way to get an empty resolved promise in Angular.

+6
source

Source: https://habr.com/ru/post/1201747/


All Articles