var rF = function(callback) {
alert("there2222");
//additional calls
$(document).trigger("lc", [callback]);
};
var pSl = function(callback) {
var func = pSR; // how to pass callback parameter in function
rF(func);
};
var pSR = function(callback, vars) {
alert(callback);
alert(vars);
};
$(document).on("lc", function(e, callback) {
alert("theaaa");
alert(callback, "ssss");
});
$('img').click(function() {
pSl("lol");
});
I guess you want to pass along callback to pSR. In that case you can use .bind:
var func = pSR.bind(null, callback);
or you put the call in another function:
rF(function() {
pSR(callback);
});
However, the choice of the parameter name is questionable, since you seem to pass a string (not a function).
Related
I've created a working recursive for loop, but it only works if my callback has no arguments. I've tried callback(arguments) and callback(...arguments).
Thanks for any help you can provide!
function loopFunc (numOfSteps, callback) {
let i = 0;
if (i >= numOfSteps) {
let i = 0
return
}
callback()
loopFunc(numOfSteps - 1, callback)`enter code here`
}
It works if the callback takes no arguments:
function noArgsHello() {
console.log('hello')
}
const thisWorks = loopFunc(3, noArgsHello);
thisWorks()
It doesn't work if the callback takes an argument:
function sayHello (input) {
console.log(input)
}
const thisDoesntWork = loopFunc(3, sayHello('hello');
thisDoesntWork()
In this way:
const thisDoesntWork = loopFunc(3, sayHello('hello');
You are not passing a callback anymore, but you are executing the sayHello function and passing the returned value of that function to the loopFunc.
What you can do in these cases, is to use the bind method of functions for passing the function with arguments:
const thisDoesntWork = loopFunc(3, sayHello.bind(sayHello, 'hello'));
Or you can pass directly a function which executes your sayHello so that the argument is still a function and it will be used as callback inside your loopFunc:
const thisDoesntWork = loopFunc(3, () => sayHello('hello'));
You almost there! It depends what is your goal here, you can use either option:
function loopFunc (numOfSteps, callback) {
let i = 0;
if (i >= numOfSteps) {
let i = 0
return
}
callback(numOfSteps)
loopFunc(numOfSteps - 1, callback);
}
function printExtraStuff(greeting) {
return (val) => { console.log('greeting ', val)}
}
function printSteps(num) {
console.log(num);
}
var test1 = function() { loopFunc(3, printExtraStuff('hi there') )};
test1()
var test2 = function() { loopFunc(3, printSteps )};
test2()
You need to use an anonymous function if you want to pass parameters in an argument -based function (callback is an argument). Your code should instead be like this:
function sayHello(input) {
console.log(input)
}
const works = () => sayHello('hello');
works();
I have a (asynchronous) function that gets the ID of a logged in user in Chrome. I'm trying to return the value of the ID with a callback but it keeps returning 'undefined'.
Before someone tries to mark this a duplicate, I used the code from here (and tried other places too): How to return value from an asynchronous callback function? But it didn't work:
function getGaia(callback) {
chrome.identity.getProfileUserInfo(function(userInfo){
var userId = userInfo.id;
callback(userInfo.id);
});
}
getGaia(function(id){
return id;
});
var gaiaId = getGaia();
I get the following error:
'callback' is a not a function
What exactly am I doing wrong / what is the correct code?
That is because you are not providing a callback.
function doSomethingLater(callback) {
setTimeout(callback, 1000);
}
console.log('This is before the callback');
doSomethingLater(function() {
console.log('This is the callback')
});
So when you are calling var gaiaId = getGaia(); you are not passing in a callback function
[Edit] This is what your code would need to look like:
function getGaia(callback) {
chrome.identity.getProfileUserInfo(function(userInfo){
var userId = userInfo.id;
// This will call the function that you pass in below
//and pass in userInfo.if as a parameter
callback(userInfo.id);
});
}
var gaiaId = getGaia(function (id) {
// id === userInfo.id from above
// Do something with the id that you pass in
});
You can think of functions like variables in JavaScript,
So you can assign a function to a variable like this:
var foo = function () { ... }
This means that you can pass this into functions like normal variables. When you pass the function in as a parameter, you are assigning the function to the name that you specify in the parameters:
var foo = function () { ... }
function hasCallback(callback) {
// The following two line do exactly the same thing:
callback(); // Using the function that you passed in
foo(); // Using the function directly
}
hasCallback(foo);
All I have done above is, instead of creating the variable foo I just created the function inline:
var foo = function () { ... }
function hasCallback(callback) {
// The following two line do exactly the same thing:
callback(); // Using the function that you passed in
foo(); // Using the function directly
}
hasCallback(foo);
// Becomes:
function hasCallback(callback) {
callback(); // Using the function that you passed in
}
hasCallback(function () { ... });
so i have a function which checks if a checksum is changed and if so it calls the callback which is provided by a parameter.
var watchFileChange = function watchFileChange(oldChecksum, callback){
// some code about checking checksum
if(oldChecksum != newChecksum){
callback()
}
}
exports.watchFileChange = watchFileChange;
my Jasmin specs looks like this.
var t = require('../server.js');
describe("watchFileChange", function() {
spyOn(t.watchFileChange, 'Callback');
var file_false = {
'foo.txt': 'd41dcccc8f00b204e9800998ecf8427e'
}
var file_true = {
'foo.txt': 'd41d8cd98f00b204e9800998ecf8427e'
}
function Callback() {
console.log("Callback Called")
}
it("Checksum is not right, it should call Callback function", function() {
watchFileChange(file_false, Callback);
expect(Callback).toHaveBeenCalled();
});
});
But it just doesn't work that way because Callback is not defined i get that. So my question is there a way to check if the by parameter provided callback is called?
You can create a fake object where you can define you callback function, and then pass it as the argument
var init = {
callback: function() {
console.log("Callback Called")
}
};
describe("watchFileChange", function() {
beforeEach(function() {
spyOn(init, 'callback');
});
//...
it("Checksum is not right, it should call Callback function", function() {
watchFileChange(file_false, init.callback);
expect(init.callback).toHaveBeenCalled();
});
});
I wondering if we can set a function containing a Callback function as parameter to another function who takes a callback too.
Example
function save(err, data, cb){};
function get(id, cb){};
get('12', save)?
Of course, a variable can be passed as argument of a function!
It may be clearer for you if you do:
// This also works with the notation `function save(...)`
var save = function(err, data, cb) {
alert('save' + err); // save12
},
get = function(id, cb) {
alert('get' + id); // get12
cb(id); // This call the "save" function
}
;
get('12', save);
Just be careful to not stack your callback too much or you will enter in the callback hell world!
Yes you can, check this example:
jsFiddle Example
jQuery(document).ready(function () {
function1({
param: "1",
callback: function () {
function2({
param : "2",
callback: function(){
alert("hello");
}
})
}
});
});
function function1(params){
alert(params.param);
params.callback();
}
function function2(params){
alert(params.param);
params.callback();
}
I hope it will be useful.
Say I write this function...
var sayHi = function() {
return "hi";
}
alert(sayHi()); will return "hi".
Now if I write it this way...
var sayHi = function(callback) {
callback("hi");
}
How do I display "hi" with this function?
Based on an example here: http://nowjs.com/doc
You pass a function to sayHi, so I imagine this:
sayHi(alert);
you must have defined some callback function or pass a anonymous function:
var sayHi = function(callback) {
callback("hi");
}
sayHi(function(message){
alert(message);
});
Try this:
sayHi(function(msg){
alert(msg)
});
Your new sayHi function doesn't return a value, so you have to perform the alert in the callback function.
sayHi(function(value) {
alert(value);
});
sayHi(function(msg) {
alert(msg);
});
You have to invert your thinking process when using callbacks. Instead of writing the next operation first, you write the next operation last.
Here in example callback is a function. So you should pass function argument.
You may do this in 2 ways:
var some_fun = function(some_str) {
alert(some_str);
}
var sayHi = function(callback) {
callback("hi");
}
sayHi(some_fun)
or you can pass function when you call it:
var sayHi = function(callback) {
callback("hi");
}
sayHi(function(some_str){
alert(some_str);
});