Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How exactly does 'this' keyword assign properties to functions in javascript?

I tried to recreate javascript promise, following this tutorial, this is the complete code from the 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;
}

im confused about the part that implements .then():

// .then
myPromise.prototype.then = function(resolveCallback){
    attachResolveCallback(resolveCallback);
}

const attachResolveCallback = function(resolveCallback){
  this.resolveCallback = resolveCallback;
}

Why is const attachResolveCallback needed, instead of just using this keyword in the prototype function?

So I replaced the .then part of the code with:

myPromise.prototype.then = function(resolveCallback){
    this.resolveCallback = resolveCallback;
}

But the code does not work and returns an error:

promise.js:4 Uncaught TypeError: this.resolveCallback is not a function
    at resolve (promise.js:4:14)
    at promise.js:25:9

I suspect this problem is due to usage of 'this' when assigning properties to functions, but im not sure about the specific reason here. Could someone enlighten me on this problem?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!