I'm wondering if there is a way to execute deferred task sequentially and conditionally. That is, i would like to perform some asynchronous task in sequence, where i would only execute the next one depending on the result of the preceding one. The result of the chain of promise should be the last to execute.
Any idea how to patch that up with Jquery deferred.
Edit:
Before i understood that ajax call properly, i started the following code for a function that verify that a value is not in the related value of another value.
function findBroaderOrNarrowerConceptInFieldSelectedList(field, value) {
var intputfields = $("#instance_" + field + "_wrapper" + " .ds-authority-value")
if (intputfields.length == 0)
return undefined;
//go over each input field and retreive the value
for (var i = 0; i < intputfields.length; i++) {
//check if our value is in the broader concepts of the input field concept
//if true return the wrapper element, if false next steps
if (is_broader_concept(value.id, $(intputfields[i]).attr('value'))) {
var WrapperdivId = "#" + $(intputfields[i]).attr('name').replace("_authority", "") + "_wrapper";
return $($(WrapperdivId)[0]);
}
//check if our value is in the narrower concepts of the input field concept
//if true return the wrapper element
if (is_narrower_concept(value.id, $(intputfields[i]).attr('value'))) {
var WrapperdivId = "#" + $(intputfields[i]).attr('name').replace("_authority", "") + "_wrapper";
return $($(WrapperdivId)[0]);
}
}
return undefined
}
The problem is that my is_broader_concept look like this:
function is_broader_concept(broaderuri, concepturi) {
return getBroaderConcepts(concepturi).then(function(data, textStatus, jqXHR ) {
var broaders = data
if (broaders.length == 0)
return false;
for (var i = 0; i < broaders.length; i++) {
if (broaders[i].uri == broaderuri)
return true;
}
});
}
Now that i have learned about deferred and promise (not so new because i did it in scala (promise and future)), I would like to adapt my first method to work with deferred. However i don't see any construct that would help me achieve what i want easily.
EDIT2:
I have found the following lib which is excellent in case you do not matter launching many parallel task. Indeed it can return on the first that succeed. It is a modification of the When. Although i think it would be nice to not have to call all of them
https://github.com/terrycojones/jquery-when2
EDIT3:
I am more looking for a lib that would without bug at all do something like in https://codereview.stackexchange.com/questions/38420/sequential-function-call-in-javascript or Conditionals on a chained deferred in jquery
EDIT4: Update based on the response by @Roamer-1888
The proper is_broader and is narrower which works with his solution
function is_broader_concept_Promise(broaderuri, field) {
var uri = field.value.substr(field.value.indexOf("http://"));
return getBroaderConcepts(uri).then(function(data, textStatus, jqXHR ) {
var broaders = data;
if (broaders.length == 0)
return $.Deferred( function( d){ d.reject(); }).promise();
for (var i = 0; i < broaders.length; i++) {
if (broaders[i].uri == broaderuri)
return field;
}
return $.Deferred( function( d){ d.reject(); }).promise();
});
}
function is_narrower_concept_Promise(narroweruri, field) {
var uri = field.value.substr(field.value.indexOf("http://"));
return getNarrowerConcepts(uri).then(function(data, textStatus, jqXHR ) {
var narrowers = data;
if (narrowers.length == 0)
return $.Deferred( function( d){ d.reject(); }).promise();
for (var i = 0; i < narrowers.length; i++) {
if (narrowers[i].uri == narroweruri)
return field;
}
return $.Deferred( function( d){ d.reject(); }).promise();
});
}
Many thanks,
M
PS: I started jQuery about a month ago.
is_broader_concept()?