2

I am trying to figure out the error for polyfill for a promise. the program doesnt work as expected when chaining second then. Can someone figure what the issue is?

tried to debug an own promise polyfill but failed where the error is occurring at, stuck at this for some time, need an expert to figure out what the issue is?

function PromisePolyfill(executor){
const PENDING = 1;
const FULFILLED = 2;
const REJECTED = 3;

let state = PENDING;
let value = null;
let handlers = [];
let catches = [];

function resolve(result){
    if( state !== PENDING) return;
    value = result;
    state = FULFILLED;
    handlers.forEach((h)=> h(value));
    return this;
}

function reject(err){
    if(state !== PENDING) return;
    value = err;
    state = REJECTED;
    catches.forEach((h)=>h(value));
}

this.then = function(callback){
    if(state === FULFILLED){
       callback(value);
       
    } else {
        handlers.push(callback);
    }
    return this;
}

executor(resolve, reject);

}

var func = (resolve,reject)=> {
     setTimeout(()=>{
        resolve('Hey I am resolved');
     },3000)
  }

 var basicPromise = new PromisePolyfill(func);
  basicPromise.then( (res) => {
    console.log(res);
   return new PromisePolyfill(func);
   } )
   .then((res)=> {
   console.log(res);
   })
  • 1
    `this.then()` must return a new promise that is chained to the previous one. It should not return the same promise. There are dozens of sample implementations of promises from scratch that you can/should probably consult to see how they really work internally. Note that `.then()` also takes two arguments (2nd one is optional) and you need to implement `.catch()` – jfriend00 Feb 05 '21 at 18:00
  • 1
    There are numerous other issues with this implementation. Please either study the specification in great detail or study an existing simple promise implementation. For example, you should not be calling `.then()` handler synchronously when registered even if the promise is already fulfilled. You need `try/catch` around the calling of all handlers? You need to support the returning of promises from calling handlers, etc... – jfriend00 Feb 05 '21 at 18:04
  • @jfriend00 thank you for pointing out the issue. I will definitely refer to those sample implementations. – Sachin Ananthakumar Feb 05 '21 at 20:31
  • Why don't you just use [one of the thousands of existing libraries](https://promisesaplus.com/implementations)? – Bergi Feb 05 '21 at 20:32
  • 1
    Have a look at [this](https://stackoverflow.com/a/15668353/1048572) or [that](https://stackoverflow.com/q/17718673/1048572) – Bergi Feb 05 '21 at 20:34
  • 1
    @Bergi I wanted to learn the internals of promises so tried to write a polyfill, yeah I do use libraries for promises :), – Sachin Ananthakumar Feb 05 '21 at 20:35
  • 1
    Also you might want to have a look at https://github.com/kriskowal/q/tree/master/design. And I'm certain I have seen various tutorials on the web that walk through the implementation of a promise library. – Bergi Feb 05 '21 at 20:38

0 Answers0