0

I have the following code:

class simplePromise {

  constructor(resolveFn, rejectFn) {
    
    console.log(resolveFn, 'resolveFn') // (resolutionFunc, rejectionFunc) => {
    //   resolutionFunc(777);
    //   rejectionFunc();
    // }
    
    console.log(rejectFn, 'rejectFn') //undefined
  }

}

const promise1 = new simplePromise( (resolutionFunc, rejectionFunc) => {
  resolutionFunc(777);
  rejectionFunc();
});

As you can see, I'm trying to pass in 2 functions to the constructor. However, when I console.logged each of them out, I noticed that both functions are registered as 1 argument. In this case, how do I separate the 2 functions?

2
  • 3
    "As you can see, I'm trying to pass in 2 functions to the constructor." No, you are passing a single function, which in turn accepts two functions as arguments. You are confusing the constructor with the callback. If you want to replicate the built-in promise API then the constructor has to pass two functions to the callback function it receives. Commented Feb 11, 2021 at 23:26
  • 1
    @FelixKling is correct. You are just passing one function to your new simplePromise instance. That function you are passing to new simplePromise takes two arguments, which you would be passing to resolveFn. You never even call resolveFn(resolutionFuncPassedHere, rejectionFunctionPassedHere)... I'm pretty sure you're still confused about the way it works, at this point. Practice, practice practice. Commented Feb 11, 2021 at 23:40

3 Answers 3

1

Are you sure that you want to pass 2 functions to the constructor?

const promise1 = new simplePromise( (resolutionFunc, rejectionFunc) => {...}

this part of your code looks more like you want a single callback function that recieves 2 functions.

Like this:

class SimplePromise {
  constructor(callback) {
    const resolveFn = (value) => {
      console.log("resolved with value", value);
    };

    const rejectFn = (error) => {
      console.log("rejected with error", error);
    }

    callback(resolveFn, rejectFn);
  }
}

const promise1 = new SimplePromise((resolutionFunc, rejectionFunc) => {
  console.log('resolutionFunc', resolutionFunc)
  console.log('rejectionFunc', rejectionFunc) //undefined

  resolutionFunc(777);
  rejectionFunc();
});

Sign up to request clarification or add additional context in comments.

Comments

1

Not that this code has anything to do with a Promise, but you should study the following structure:

class simplePromise{
  constructor(resolveFn, rejectFn){
    this.prop1 = 'prop1value'; this.prop2 = 'prop2value';
    resolveFn.call(this, 'Passing to first function argument'); // calling in simplePromise context
    rejectFn.call(this, 'Passing to second function argument');
  }
}

const promise1 = new simplePromise(function(funcOneArg){
  console.log(funcOneArg);;
  console.log("this.prop1 = '"+this.prop1+"';"); // see why `.call(this` is in `constructor`
  console.log("this.prop2 = '"+this.prop2+"';");
},
function(functionTwoArg){
  console.log('-'.repeat(45));
  console.log(functionTwoArg);
});

Comments

0

So when you pass the arrow function to the new simplePromise constructor, that entire function is the first argument (resolveFn). In order to instantiate this the way you want, you need to define the resolutionFunc and rejectionFunc and then do it like so:


const resolutionFunc = () => {do stuff}
const rejectionFunc = () => {do stuff}

const promise1 = new simplePromise(resolutionFunc, rejectionFunc)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.