Reputation: 5924
I am trying to store callbacks in my database, and use them later from another session.
Is it possible?
Lets make some code for example:
// One session
var callback = () => {
console.log("Hey! This is a callback!");
}
db.store("myCallback", callback);
// Another session
db.get("myCallback")(); // Output: "Hey! This is a callback!"
EDIT - Just to clarify my context, we can refer to this question: Question
Upvotes: 2
Views: 620
Reputation: 2224
It would work best to store the parts if the callback functions that differ.
var callbackParams = {
message : "Hey! This is a callback!"
}
db.store("myCallback", callbackParams);
// Another session
var handleCallback = (params) => {
console.log(params.message);
}
handleCallback(db.get("myCallback"));
The worst way is to use eval
. Do not use eval
as it could be easily exploitable leaving your program and system vulnerable for easy attacks.
Upvotes: 1
Reputation: 2454
You could use the javascript Function class.
As Alex Blex said, to implement this, you would have to store each callback as a string of javascript code in the database, and then retrieve it as a string. You pass this in as an argument for the Function(...)
constructor.
That being said, this isn't a very efficient way of doing things. If you could store the result of the function in the data base, that would save a lot of space.
Upvotes: 2
Reputation: 3624
I'd make scene if you had something like this:
var myObj = new function() {
// One session
this.callback1 = () => {
console.log("Hey! This is a callback1!");
}
this.callback2 = () => {
console.log("Hey! This is a callback2!");
}
}();
db.store("myCallback", "callback1");
// Another session
myObj[db.get("myCallback")](); // Output: "Hey! This is a callback1!"
Upvotes: 0