Jquery: scope in a .when .done function [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to figure out how to access a variable inside a .when.done function. here's the example:
var colviews = {
1: true,
2: true,
3: false
}
$.when( // run only -when- these script get loaded
$.getScript( "/mckinney_images/jquery.tablesorter.all.js" )).done(function(){
$(function(){
console.log(colviews); // How do I get the colviews variable here?
});
});
I have a basic understanding of scope but not sure how it applies inside a when function.

I have a basic understanding of scope
Good! So you know what scope is, how closures work and how var works.
…but not sure how it applies inside a when function.
Simple: It's no different! Yes, the done callback function is invoked asynchronously, but that doesn't change how scope works.
You just are getting the colviews variable there successfully (if it doesn't log the expected value, that means it was overwritten elsewhere or the callback is never called because $.getScript failed).
$.when does no magic (and in fact, in your example it's not needed at all, your code works the same when you omit it).

Related

What is "this.return" in JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
While in VS Code, I hit the tab after typing return and the VS Code Intellisense auto-completed "return" to "this.return".
I've never seen this before and cannot find any documentation on it.
Does anybody know if this is actually a thing, or if VS Code possibly lost it's marbles.
Note: the object I was working in does not have any properties or functions called "return".
Well, an object could have a property called return:
const obj = {
return: () => { console.log('↩️'); }
};
obj.return();
Were you in the context of a class that had a return property, or maybe its superclass did?
(Honestly it seems more likely VS Code was just being weird.)
You can run console.log("this", this) in most modern browsers this will return the JSON data of this (the variable this refers to the current instance of declared object FYI), and console.log(typeof(this.return)) and it will likely return function (if you get undefined just change it from this.return to return;)
Likely the object either has a property called return that is a function, or something has gone wrong in the autocomplete.

Javascript: How to set in a callback an optional parameter with the content of a variable [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have to provide a callback as a parameter to a function.
The callback will be called by the function with certain parameters. But I need to add some extra variable data on each call.
So let's say the callback is: function(par1, par2, my_par=my_variable) { ...
par1 and par2 are the parameters that the function will pass when invoking the callback. And my_par is the optional parameter I need to have differnent values on each call so it can be accessed from inside the callback.
It doesn't work. How can I do it?
Thanks.
Hard to make out your actual problem since you didn't provide a working example of your problem, but this may help:
function executeTheCallback(cb) {
cb();
}
function theCallback(greeting, name = 'Anonymous') {
console.log(`${greeting} ${name}!`);
}
executeTheCallback(theCallback.bind(null, 'Hello')); // Hello Anonymous!
executeTheCallback(theCallback.bind(null, 'Hello', 'World')); // Hello World!

Bluebird Error: Generator Function Must Be A Function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am getting the error:
bluebird.js:2118 Uncaught TypeError: generatorFunction must be a function
But I don't understand why this error is occurring, as I am passing it a generatorFunction.
export class Welcome {
fetch() {
Promise.coroutine(this.userData());
}
userData = function* getData() {
this.lotsData = yield this.testApi.getMock();
this.lotsData = JSON.stringify(this.lotsData, null, 4);
}
}
So a click event calls fetch() and that calls this.userData(). This is the console dump of this.userData:
GeneratorFunctionPrototype {}
_invoke: invoke(method, arg)
__proto__: GeneratorFunctionPrototype
Which tells me it most certainly is a generator. I am using all of this in an aurelia class if that somehow makes any difference (which it shouldn't I don't think). So why is it erroring that the generatorFuction must be a function?
The limited amount of code you posted is riddled with errors, but there is not enough to know what is correct and what is incorrect.
One thing is for sure this.userData() is a function call and not a reference to a function, which is what .coroutine() is expecting.
Whatever type returned by this.testApi.getMock() is what is being yielded, which we have no idea given what you posted, but that is probably not even relevant at this point, because what is returned is probably unknown/null anyway because you are assigning the yield to a variable of questionable scope. Your code is nowhere near the example from the documentation
PingPong.prototype.ping = Promise.coroutine(function* (val) {
console.log("Ping?", val)
yield Promise.delay(500)
this.pong(val+1)
});
The documentation has a very clear and well defined example you should follow, explicitly until you understand what you are actually doing.

Avoiding 'too much recursion' error [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Basically what I'm trying to do boils down to
function a() {
// Do stuff that waits for things etc
b(a);
}
function b(f) {
f()
}
function a() { b(a); }; function b(f) { f(); }; a()
That will cause a too much recursion error after a while though. Apparently javascript doesn't support tail-recursion so that won't work either. I can't think of any way to use a loop here either since I don't want the code executed immediately. So any way to do this or is it impossible?
Also I apologize if this has been asked before, couldn't find anything that helped.
Edit: Also before anyone asks, no I'm not actually using setTimeout so setIntervall isn't an option.
Edit again: Alright hope this shows what I'm trying to do better. I need to call the same code after waiting for things to complete again without actually putting it in a loop and blocking the program.
Because each call to a() returns before the next invocation, there is no recursion going on. The runtime system repeatedly makes individual calls to the function, and there won't ever be more than one call going on at any given time.
Breaking it down:
Somewhere, your code calls a() to start the cycle
That call to a() "does stuff" and then invokes setTimeout()
The system arranges for the timeout, and that call returns immediately
The original call to a() completes
100 milliseconds later, the timer fires and the runtime invokes a()
The cycle just repeats after that.
edit Maybe I should be more explicit: the word recursion refers to a process wherein a function invokes itself (synchronously) either directly or indirectly. Like:
function fibonacci(n) {
return fibonacci(n - 1) + fibonacci(n - 2);
}
What we have in the code posted in the OP is quite different. There's no call to a() being made from within that function (unless the OP left out some important details). Instead, a reference to the function is being handed over to the system. The call via that reference won't take place until a long, long time after the original call has finished.

How can I pass a function as parameter in javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to create a function with a callback function, but the solutions I've found does not allow to set the function.
I'll explain:
this is the solution I've found:
function callbackFunction()
{
alert("hello world");
}
function myFunction(callback)
{
callback()
}
myFunction(callbackFunction()) /* this works */
this is what i need:
function myFunction(callback)
{
callback()
}
myFunction(function(){alert("hello world");}); /* this doesn't work */
Some ideas?
Thank you
myFunction(callbackFunction()) /* this works */
No it doesn't. At least not in the way you think it does. This is:
Invoking callbackFunction
Passing the result of callbackFunction to myFunction
You're probably getting an error from within myFunction when it tries to invoke callback since that's not a function. But you're ignoring that because you see the alert() and think it works. The alert() happened before myFunction was invoked.
You want to pass it as a function reference, not a function invocation:
myFunction(callbackFunction) /* this works */
This would produce the same visible result (the alert()) but in the expected order of operations and without the error.
myFunction(function(){alert("hello world");}); /* this doesn't work */
You sure about that? If that is indeed "not working" for you then there must be more to the problem that you're not sharing with us, because that code works as-is.

Categories

Resources