I would like to make a function to pass a list name, if it exists, then append random number to its name and create a new one, if there's no list with the passed list name, then create it.
So I am doing like the code below:
var listName;
function _addList(listName){
var promise= _checkIfExists(listName);
promise.done(function(){
//Add the list to the collection
});
}//end function
function _checkIfExists(listName){
var mypromise = _getListByName(listName);
var dfd = $.Deferred();
mypromise.then(
function(data, status, jqXHR){
listName= generateName(); //list exists, then generate another name
},
function(jqXHR, status, error){
listName= listName; //object doesn't exist, use same name
});
return dfd.promise();
};
function _getListByName(listName){
var url = "site url..../web/lists/getByTitle.."
return ($.ajax({..}).promise();
}
But once am trying to get a list that doesn't exist, I am having a 404 error and it stops. Shouldn't the code just calls the fail part of the promise and continue with the execution after return dfd.promise()
?
return dfd.promise()
has already executed. Add loads ofconsole.log
so you see the orderpromise.done
. Because you split it in seperate functions you make it difficult to read. Do it all in one function with promise chains first. If that works split it into seperate functions (but only when you want to reuse code)