JavaScript handle a function that returns a promise or a null value function handle to symbolic matlab,function handle to string matlab,

I have defined a function as following :

function getCurrentComponent(){
    if($rootRouter._currentInstruction){
        return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(function (data) {
            return data.component.componentType;
        });
    }else{
        return null;
    }
}

To call this function I did as following :

factory.getCurrentComponent().then(function (data) {
    ...
});

The problem is when the getCurrentComponent function returns a null value, the following error is generated :

Cannot read property 'then' of null

How can I solve this ?

Edit:

I forgot to say that I'm limited to use ES5, so I can't work with the object Promise

Answer:1

I cant use the object Promise in ES5.

Use the AngularJS $q Service:

function getCurrentComponent(){
    if($rootRouter._currentInstruction){
        return $rootRouter.recognize($rootRouter._currentInstruction.urlPath).then(function (data) {
            return data.component.componentType;
        });
    }else{
        ̶r̶e̶t̶u̶r̶n̶ ̶n̶u̶l̶l̶;̶
        return $q.reject(null);
    }
}

AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

The $q Service is a Promises/A+-compliant implementation of promises/deferred objects thst is integrated with the AngularJS framework and its digest cycle.

Answer:2

The following Promise behaves as expected, the Promise in the return statement gets fulfilled and the then right afterwards gets executed before the last call of then: (new Promise(resolve => ...

The following Promise behaves as expected, the Promise in the return statement gets fulfilled and the then right afterwards gets executed before the last call of then: (new Promise(resolve => ...

Here is my code function consec(string) { for (let letter of string) { console.log(letter); } } console.log(consec("zoo")); the result is "z" "o" "o" undefined This is the link of my ...

Here is my code function consec(string) { for (let letter of string) { console.log(letter); } } console.log(consec("zoo")); the result is "z" "o" "o" undefined This is the link of my ...

I am using SVG filter feMorphology's dilate to implement shadow spread for a shape. Am expecting below result. <?xml version="1.0" encoding="UTF-8"?> <svg width="188px" height="209px" ...

I am using SVG filter feMorphology's dilate to implement shadow spread for a shape. Am expecting below result. <?xml version="1.0" encoding="UTF-8"?> <svg width="188px" height="209px" ...

I use Webix 2.5.14. There was a problem with a component Richselect. In this form there is a richselect with options. webix.ui({ view: "form", id:"addAccessForm", borderless: true, elements: [ { ...

I use Webix 2.5.14. There was a problem with a component Richselect. In this form there is a richselect with options. webix.ui({ view: "form", id:"addAccessForm", borderless: true, elements: [ { ...