doA(function(){
doB();
doC(function() {
doD();
})
doE();
});
doF();
excution order
doA() -> doF() -> doB() -> doC() -> doE() -> doD()
I don't understand why the execution order is as above...:(
The order the functions are executed depends on how doA and doC call their respective callbacks
Synchronously
function doA(cb) { console.log('doA'); cb(); }
function doB() { console.log('doB'); }
function doC(cb) { console.log('doC'); cb(); }
function doD() { console.log('doD'); }
function doE() { console.log('doE'); }
function doF(cb) { console.log('doF');}
doA(function(){
doB();
doC(function() {
doD();
})
doE();
});
doF();
or Asynchronously
function doA(cb) { console.log('doA'); setTimeout(cb); }
function doB() { console.log('doB'); }
function doC(cb) { console.log('doC'); setTimeout(cb); }
function doD() { console.log('doD'); }
function doE() { console.log('doE'); }
function doF(cb) { console.log('doF');}
doA(function(){
doB();
doC(function() {
doD();
})
doE();
});
doF();
Related
I have the following function, that does a service call with a promise and a .finally:
myService.getStuff().then(function() {
this.doStuffWhenServiceOK();
}, function () {
this.doStuffWhenServiceFails();
}).finally(function() {
this.doFinally();
});
I am spying on this service with the following spy:
spyOn(myService, 'getStuff').and.callFake(function() {
return {
then: function (succesFn, errorFn) {
return succesFn();
}
};
});
The problem is that the test complains that the .finally is not known. Just adding it after .then does not seem to be a solution...
return {
then: function(successFn) {
return successFn();
},
finally: function(successFn) {
return successFn();
}
}
Who knows how to chain .then and .finally in the callFake spy?
I work with Angular 1.
Return a finally function.
function then(succesFn, errorFn) {
succesFn();
return {finally:function() {}};
}
is there anyway to return a value in a function with a callback function?
function getnextseq(){
autoincrement.findOneAndUpdate({ _id:"userid"}, { $inc: { seq:1}},function(err,data){
console.log(data.seq)
})
return data.seq;
}
console.log(getnextseq());
Simply return data.seq won't work since findOneAndUpdate is asynchrouse. You need either pass a callback function or use promise
function getnextseq(cb) {
autoincrement.findOneAndUpdate({ _id:"userid"}, { $inc: { seq:1}},function(err,data){
cb(data.seq);
})
}
getnextseq(function(seq) {
console.log(seq);
}
);
Or promise way
function getnextseq() {
return autoincrement.findOneAndUpdate({ _id:"userid"}, { $inc: { seq:1}}).exec();
}
getnextseq().then(function(seq) {
console.log(seq)
});
How to i handle when there is no response from api
service.js
app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
return {
getDropDownData: function (callback) {
$http.get(CD_CONTEXT_VOLTAGE_URL).success(callback);
}
}
});
controller.js
app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {
getFormData();
function getFormData() {
dropdownService.getDropDownData(function (results) {
$scope.contextlist = results.context;
$scope.cdlst = results.coordinationType;
$scope.voltageList = results.constraintValue;
});
};
}]);
The above code handles only success conditions.Could somebody help me with the code so that i can handle the error condition
If you check angular $http documentation, you will see that $http.get() returns a promise which has an .error() method. That is where you give some function which handles the error for you.
app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
return {
getDropDownData: function (callback) {
$http.get(CD_CONTEXT_VOLTAGE_URL).
success(callback).
error(errorCallback);
}
}
});
In your service.js
app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
return {
getDropDownData: function (callback, errorCallback) {
$http.get(CD_CONTEXT_VOLTAGE_URL)
.success(callback)
.error(errorCallback);
}
}
});
While in your controller.js
app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {
getFormData();
function getFormData() {
dropdownService.getDropDownData(function (results) {
$scope.contextlist = results.context;
$scope.cdlst = results.coordinationType;
$scope.voltageList = results.constraintValue;
}, function(error){
console.log('Error while getting response from the REST call');
});
};
}]);
This should do the trick.
In your service you can register .error callback() as well along with, .success() call back.
The updated code will be:
service.js
app.factory('dropdownService', function ($http,CD_CONTEXT_VOLTAGE_URL) {
return {
getDropDownData: function (successcallback,errorCallback) {
$http.get(CD_CONTEXT_VOLTAGE_URL)
.success(successcallback)
.error(errorCallback);
}
}
});
controller.js
app.controller('loadDropDownController', ['$scope','dropdownService',function($scope,dropdownService) {
getFormData();
function getFormData() {
dropdownService.getDropDownData(function (results) {
$scope.contextlist = results.context;
$scope.cdlst = results.coordinationType;
$scope.voltageList = results.constraintValue;
},function(){
//do something on error
});
};
}]);
How to rewrite this with using async_ajax()?
function some_event_handler () {
if (condition) {
sync_ajax();
ajax_dependent_code1;
} else {
ajax_independent_code;
}
ajax_dependent_code2;
}
I did it in the following way:
function some_event_handler () {
async_ajax(callback_function () {
if (condition) {
ajax_dependent_code1;
} else {
ajax_independent_code;
}
ajax_dependent_code2;
});
}
I hope it will be okay...
I have two nearly identical functions, but I'm not sure how I can refactor this so they can share the same logic.
function Content_chkClick(obj) {
var frame = $('#iFM')[0];
if (frame.contentWindow.Content_chkClick) {
frame.contentWindow.Content_chkClick(obj);
} else {
$('.TabContent', frame.contentWindow.document).each(function () {
var frame = this;
if (frame.contentWindow.Content_chkClick) {
frame.contentWindow.Content_chkClick(obj);
}
});
}
}
function Content_invokeClickEvent(id) {
var frame = $('#iFM')[0];
if (frame.contentWindow.Content_invokeClickEvent) {
frame.contentWindow.Content_invokeClickEvent(id);
} else {
$('.TabContent', frame.contentWindow.document).each(function () {
var frame = this;
if (frame.contentWindow.Content_invokeClickEvent) {
frame.contentWindow.Content_invokeClickEvent(id);
}
});
}
}
Ultimately, what I want to be able to do is just have something like
function Content_chkClick(obj) {
someCommonFunction(Content_chkClick, obj);
}
function Content_invokeClickEvent(id) {
someCommonFunction(Content_invokeClickEvent, id);
}
function Content_xClick(fname, obj) {
function callIfPossible(frame) {
if (frame.contentWindow[fname]) {
frame.contentWindow[fname](obj);
return true;
}
}
if (!callIfPossible($('#iFM')[0])) {
$('.TabContent', $('#iFM')[0].contentWindow.document).each(function () {
callIfPossible(this);
});
}
}
Ultimately, you should be able to do is just something like
function Content_chkClick(obj) {
Content_xClick("Content_chkClick", obj);
}
function Content_invokeClickEvent(id) {
Content_xClick("Content_invokeClickEvent", id);
}