Previously I had
MyClass.prototype.method1 = function(data1) {
return this.data111.push(data1);
};
MyClass.prototype.method2 = function(i) {
var data = this.method1(i);
if (data.condition1 != null) {
data.onEvent1(this);
}
return $(data.element).someMethod123("data123");
};
MyClass.prototype.method3 = function() {
var data1 = this.method1(this._data1);
return this.someMethod123(step.data1);
};
MyClass.prototype.ended = function() {
return !!this.getState("end");
};
MyClass.prototype.getState = function(key) {
var value = $.cookie(key);
this._options.afterGetState(key, value);
return value;
};
How do I make async using callback functions? I guess it should be so:
MyClass.prototype.method1 = function(data1, callback) {
if(callback){
callback(this.data111.push(data1));
}
else{
return this.data111.push(data1);
}
};
MyClass.prototype.method2 = function(i, callback) {
var data = this.method1(i);
if (data.condition1 != null) {
data.onEvent1(this);
}
if(callback){
callback($(data.element).someMethod123("data123"));
}
else{
return $(data.element).someMethod123("data123");
}
};
MyClass.prototype.method3 = function(callback) {
var data1 = this.method1(this._data1);
if(callback){
callback(this.someMethod123(step.data1));
}
else{
return this.someMethod123(step.data1);
}
};
MyClass.prototype.ended = function(callback) {
if(callback){
callback(!!this.getState("end", /*what should be here and what should it does?*/));
}
};
MyClass.prototype.getState = function(key, callback) {
var oldThis = this;
setTimeout(function(){
value = $.cookie(key);
callback(value, oldThis);
oldThis._options.afterGetState(key, value);
},
0);
};
I definitely have missed something because I never used async functions in javascript before. So it that?
And, as I understood, to make a functions async, I basically should add one more parameter as a callback function and get rid of return, should not I?