Bluebird Error: Generator Function Must Be A 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 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.

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!

How this works? I can't understand this snippet [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
let unrealFunctionToUnderstand = () => {
let tryToUnderstandThis = () => 666;
console.log('I\'m calling once time! :D');
return tryToUnderstandThis;
}
let hardcoreLesson = unrealFunctionToUnderstand();
console.log(hardcoreLesson());
console.log(hardcoreLesson());
I cant understand this code, my friend send me this...
unrealFunctionToUnderstand is a function. When called it logs "I'm calling once time! :D".
It also returns another function (tryToUnderstandThis) when called.
After defining this function you are (1) calling it unrealFunctionToUnderstand(), then (2) assigning it's returned value (tryToUnderstandThis) to hardcoreLesson. Then you are calling hardcoreLesson (reference to tryToUnderstandThis) twice and logging the result.
So you are calling unrealFunctionToUnderstand once, and it logs "I'm calling once time! :D", then calling tryToUnderstandThis twice, and it logs "666" twice.
Can you notice how I "ran" this code on paper? This is how you answer questions like this yourself. You interpret the code the same way the browser would, on paper. It becomes easier to pinpoint language constructs you don't understand or know yet, so you can first learn / ask about those. Then, if you understand each part, it becomes clear what is executed and why.
everything in javascript is an object, including functions. Which means you can return a function from a function.
That is exactly what unrealFunctionToUnderstand() is - it is a function which returns a function.
So, you call it once:
let hardcoreLesson = unrealFunctionToUnderstand();
Hence the console output only displays once. And you now have a reference to a function which simply returns the value 666.
let tryToUnderstandThis = () => 666;
....
return tryToUnderstandThis;
When you execute that, you get back the response.
hardcoreLesson store the value return by this function unrealFunctionToUnderstand.
So unrealFunctionToUnderstand calling only one time.
as hardcoreLesson store the value it's shows 2 times as it's called two time.
If you are familiar with any other programming language like C or Java, then you will be familiar with the following syntax.
function functionName(argument1, argument2, ...) {
// function body
// return statement
}
The new version of javascript introduces the arrow operator => to reduce your effort of writing single line functions. This is similar to lamda/inline function used mostly for single line functions.
So if you have a following function
function increment (value) {
return value + 1;
}
you can replace it with
increment(value) => value + 1
The other answers should help you understand how the function are also objects and how it can be called in different ways.
Some helpful links
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-closure-b2f0d2152b36

Variable undefined inside JavaScript For-In loop [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 6 years ago.
Improve this question
I stumbled upon this puzzling error when looping over JavaScript objects using the for..in construct. I have a set of dummy data similar to this:
{
"12345678" : {
...
},
"12345679" : {
...
},
"12345680" : {
...
}
}
Maybe it matters, this object is retrieved from a Firebase database using the response's built-in .val() method. It is a regular js object.
However, when traversing the object, the variable declared inside the for..in loop is somehow undefined. I've removed all surrounding code except the loop, and the error still occurs:
for (key in data) {
console.log(data[key]);
}
// Throws ReferenceError: key is not defined
I am really baffled. How is this possible? I thought the variable passed to for..in was always available (or at least defined) inside the loop.
As mentioned in my comment, you'll probably need to define key; otherwise, you'll (probably) define a global variable, which may or may not work correctly. Also, to avoid unrelated properties from appearing in your loop, use hasOwnProperty:
for (var key in data) {
if (!data.hasOwnProperty(key)) {
continue;
}
console.log(data[key]);
}
See How do I loop through or enumerate a JavaScript object? for further information.
you just don't use the right syntax for for..in statement.
You need to define key variable with var (or let if you use ES6): in order to use it in the statement:
for(var key in data){
console.log(data[key])
}

Jquery: scope in a .when .done 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 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).

Categories

Resources