javascript - Promise.resolve object is not a constructor - Stack Overflow

I have an object with two methods.foo.publicMethod() will call foo.privateMethod() internally.For exam

I have an object with two methods. foo.publicMethod() will call foo.privateMethod() internally.

For example:

foo.prototype.publicMethod = function() { 
   return this.privateMethod()
       .then(/* Do some other stuff */); 
};

In order to test the public method in isolation I am stubbing the private method, by making it return an empty promise. For some reason, if I assign

foo.privateMethod = () => Promise.resolve();

everything works out fine, however doing

foo.privateMethod = Promise.resolve;

produces an error message: TypeError: object is not a constructor

I can't see how these two lines of code would produce a different result. Yes, one is technically wrapping Promise.resolve once, but I don't see how this should affect the end result. Any ideas what the difference might be?

I have an object with two methods. foo.publicMethod() will call foo.privateMethod() internally.

For example:

foo.prototype.publicMethod = function() { 
   return this.privateMethod()
       .then(/* Do some other stuff */); 
};

In order to test the public method in isolation I am stubbing the private method, by making it return an empty promise. For some reason, if I assign

foo.privateMethod = () => Promise.resolve();

everything works out fine, however doing

foo.privateMethod = Promise.resolve;

produces an error message: TypeError: object is not a constructor

I can't see how these two lines of code would produce a different result. Yes, one is technically wrapping Promise.resolve once, but I don't see how this should affect the end result. Any ideas what the difference might be?

Share Improve this question edited Jan 18, 2017 at 16:53 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Jan 18, 2017 at 16:52 Tomasz KaminskiTomasz Kaminski 9107 silver badges14 bronze badges 1
  • produces an error message — when? where? That line by itself is not an error. – Pointy Commented Jan 18, 2017 at 16:55
Add a ment  | 

2 Answers 2

Reset to default 6

The two are not exactly the same. In the working version the context of the resolve call is the Promise object. In the second version, the context is whatever context privateMethod is called with, which would be foo when you call it as foo.privateMethod().

To make sure you have the context set correctly with the second syntax, use bind:

foo.privateMethod = Promise.resolve.bind(Promise);

function Foo() {}

Foo.prototype.publicMethod = function() { 
   return this.privateMethod(); 
};

var foo = new Foo();
foo.privateMethod = Promise.resolve.bind(Promise);

// Test it
foo.publicMethod().then ( _ => console.log('done')); 

This is a mere guess but somewhere in your code you could be overwrititing foo, you first define it as a constructor function but then you overwrite it with an instance.

My guess is based on how you use foo, first you assign to its prototype

foo.prototype.publicMethod = ...

but then suddenly it is an instance

foo.privateMethod = ...

while one would expect

foo.prototype.privateMethod = ...

If somewhere later you try to use foo, which is an instance, as it is a constructor function:

var f = new foo(); // foo is not a constructor function

It could however be helpful to see the exact line that causes the issue, the assignment you show in two versions can't just end with the error message you posted.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745565642a4633376.html

相关推荐

  • javascript - Promise.resolve object is not a constructor - Stack Overflow

    I have an object with two methods.foo.publicMethod() will call foo.privateMethod() internally.For exam

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信