How to pass 'this' into a Promise without caching outside? [duplicate] - javascript

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 3 years ago.
I have a variable called LangDataService.isDataReady that is a Promise wawiting to be resolved. Upon resolve some logic will happen. How can I pass this into that Promise?
LangDataService.isDataReady.then(function () {
this.modalOn()
});
I know i can cache var self_ = this; but I'm curious of other alternatives?

LangDataService.isDataReady.then(function () {
this.modalOn()
}.bind(this));

Related

Javascript wrapped function and this keyword [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 1 year ago.
I'm learning about "bind", and I found there is another way of using the wrap function, without using "bind". I can code like below.
let user = {
firstName: "John",
sayHi() {
alert(`Hello, ${this.firstName}!`);
}
};
//using wrap function instead of bind
setTimeout(function() {
user.sayHi(); // Hello, John!
}, 1000);
If you use setTimeout(user.sayHi, 1000); instead of wrap function, it doen't work. I know why.
But I can't understand, why does it work when you use wrap function?

Uncaught TypeError is not a function when I call a method from another method using setInterval in Javascript [duplicate]

This question already has answers here:
setTimeout and "this" in JavaScript
(5 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 2 years ago.
I'm having this error when I call a method from another method in a class. This method is been called from setInterval.
class App {
b() {
console.log("BBBBBBBB")
}
t() {
console.log("TTTTTTT")
this.b();
}
}
const t = new App();
setInterval(t.t, 1000);
You need to bind the method to the variable so the value of this stays constant. Read this page for more information.
setInterval(t.t.bind(t), 1000);

undefined function when accessed from asynchronous function [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
How to access the correct `this` inside a callback
(13 answers)
Closed 5 years ago.
I don't understand, why when I set a function to an object instance, whenever it is accessed from something asynchronous, like setTimeout or a promise, it is undefined. Can someone please explain? Is there a workaround?
Thanks
function Animal() {
this.check = function () {
writeln(typeof this.test);
}
setTimeout(this.check, 1000); // returns undefined
}
var animal = new Animal();
animal.test = function () {
}
animal.check(); // returns function
Because you're losing context here:
setTimeout(this.check, 1000);
So therefore this inside check will be window, which does not have a test property. Maybe do:
setTimeout(this.check.bind(this), 1000);
Side note: Having dynamically assigned functions is a performance killer and therefore bad style. There's always a better way...

How to handle returning an inner callback variable? [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 5 years ago.
How do I access a variable inside a callback without reassigning it to a variable first?
For example, the following code works:
let volume = 0;
loudness.getVolume((err, vol) => {
volume = vol;
});
But what if I wanted it to be assignable directly to a const. The following returns undefined:
const volume = loudness.getVolume((err, vol) => vol));
The short answer is you can not. The callback function exists in it's own scope isolated from the rest of the code. The only way to extract that info to be used in the rest of the code is to assign it to a variable that exists in a parent scope.
In simple terms do what you did in your first example.

Variable Scope inside a promise jquery [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 8 years ago.
Imagine you are doing the following inside a javascript function called Fetch.
function Fetch(context)
{
var request = $.ajax({...});
request.done(function(response)
{
// it looks like context is visible here and in Scope.
//
});
}
Can you explain why context is visible inside the callback function.?
context is local to Fetch. request is declared inside of Fetch, therefore context is available inside request

Categories