I'm doing a deep dive into ES6 native Promises. Along the way I came across some articles that quote Douglas Crockford design choices regarding not using things like new
, Object.create
, this
, etc. Some people are advocating the use of Factory Functions over constructors. I have also learnt that there is a lot of heated debate regarding those choices. So to avoid having this question listed as not constructive, I'd like to ask this specific question.
How can I create a factory function for a Promise without using new?
// from [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
var promise1 = new Promise(function(resolve, reject) {
setTimeout(resolve, 100, 'foo');
});
console.log(promise1);
// expected output: [object Promise]
This is also true for using things like building a new Headers()
for a fetch
.
At some point in the factory function, I'm going to have to write:
new Promise();
Are DC and the other authors referring to custom objects only, and not builtins? What about all the other API's that require the use of new?