JS: Creating a Promise class — how does function take an arg?

I am looking at an implementation of a Promise class (at bottom). The constructor takes a function, f.
So if I have a regular function I want to pass in:

function myFunc() {
    console.log("My Func");
}

If a Promise looks like this :

let promise = new Promise(resolve => {
    myFunc();
}

then I am confused as to what f represents now though in the constructor of the class below. It appears as though resolve is the argument, not a function, f.

*Promise Class

class Promise {
    constructor(f) {
         this.onResolves = [];
         this.status = "pending";
         const resolve = value => {
            debugger
           this.status = "resolved";
           this.value = value;
           this.callOnResolves();
         };
         f(resolve);
    }
    callOnResolves() {
         if (this.status === "resolved") {
           this.onResolves.forEach(onResolve => onResolve(this.value));
           this.onResolves.length = 0;
         }
    }
    then(onResolve) {
         return new Promise(resolve => {
           this.onResolves.push(value => resolve(onResolve(value)));
           this.callOnResolves();
         });
    }
}

85 thoughts on “JS: Creating a Promise class — how does function take an arg?”

  1. What’s up to every body, it’s my first pay a quick visit of this website; this
    web site contains remarkable and truly fine information designed for
    visitors.

    Reply
  2. Great beat ! I wish to apprentice even as you amend your site, how
    can i subscribe for a weblog web site? The account helped me a appropriate deal.
    I had been a little bit familiar of this your broadcast
    offered brilliant transparent idea scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery

    Reply
  3. Can I just say what a comfort to find someone that genuinely knows what they’re discussing online.
    You certainly understand how to bring an issue to light and
    make it important. A lot more people need to look at this
    and understand this side of the story. I was surprised you aren’t more popular since you most
    certainly have the gift. quest bars http://tinyurl.com/49u8p8w7 quest
    bars

    Reply
  4. My programmer is trying to convince me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using Movable-type on several websites
    for about a year and am nervous about switching to another platform.
    I have heard great things about blogengine.net. Is there a way
    I can transfer all my wordpress posts into it? Any kind of help would be greatly appreciated!

    Reply

Leave a Comment