Traté de recrear la promesa de javascript, siguiendo este tutorial , este es el código completo del tutorial:
function myPromise(callback) { let resolve = function(value){ this.resolveCallback(value); } callback(resolve) } // .then myPromise.prototype.then = function(resolveCallback){ attachResolveCallback(resolveCallback); } const attachResolveCallback = function(resolveCallback){ this.resolveCallback = resolveCallback; }estoy confundido acerca de la parte que implementa .then():
// .then myPromise.prototype.then = function(resolveCallback){ attachResolveCallback(resolveCallback); } const attachResolveCallback = function(resolveCallback){ this.resolveCallback = resolveCallback; }¿Por qué se necesita const attachResolveCallback, en lugar de solo usar esta palabra clave en la función prototipo?
Así que reemplacé la parte .then del código con:
myPromise.prototype.then = function(resolveCallback){ this.resolveCallback = resolveCallback; }Pero el código no funciona y devuelve un error:
promise.js:4 Uncaught TypeError: this.resolveCallback is not a function at resolve (promise.js:4:14) at promise.js:25:9Sospecho que este problema se debe al uso de 'esto' al asignar propiedades a las funciones, pero no estoy seguro del motivo específico aquí. ¿Alguien podría iluminarme sobre este problema?