4

I'm trying to use a promise but get a type error: Promise is not a constructor.

Here's the promise:

        var Promise = new Promise(
            function (resolve,error) {
                for (var key in excludeValues) {
                   /* some ifs */
                    minVal = someValue 
                    ........
                    ........
                    }


                resolve(errors)
            });
            Promise.then(
            function(data){
                if (minVal > maxVal)
                {
                    errors.minMax.push(
                        'minMax'
                    )
                }

                if (gapVal > minVal * -1)
                {
                    errors.minMax.push(
                        'gapVal'
                    )
                }
                return (errors.minMax.length == 0 && errors.zahl.length == 0 && errors.hoch.length == 0 && errors.niedrig.length == 0)
            }
        );

Can someone tell me what I'm doing wrong?

baao
  • 71,625
  • 17
  • 143
  • 203

3 Answers3

8

With var Promise you declare a local variable in your scope. It is initialised with undefined and shadows the global Promise constructor. Use a different variable name

var promise = new Promise(…);
promise.then(…);

or none at all

new Promise(…).then(…);
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

problem is you are overwriting Promise function, so when you execute code second time "Promise" is not function anymore.

so change variable name as shown below

var promisevariable = new Promise(
        function (resolve,error) {
            for (var key in excludeValues) {
               /* some ifs */
                minVal = someValue 
                ........
                ........
                }


            resolve(errors)
        });
0

I had a very similar problem that gives an almost identical error message. I declared:

var promise = new promise (...) instead of new Promise(...).

That mistake will give a very similar error message, as follows:

promise is not a constructor (note the lowercase promise).

In this case, the error is because you're referencing the variable you've declared (promise), instead of invoking the Promise constructor.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206