Возможно ли вызвать функцию в JavaScript , которая снова вызывает себя , и после повторного вызова, наконец запускает начальный обратный вызовфункция?
Я объясню, что я имею в виду в коде ...
//on page load
getProviderNextAppointment(null, function(nextAppointment) {
otherFunction(); //<----- how can I always end up back here, no matter what?
});
//getProviderNextAppointment function
function getProviderNextAppointment(startDate, callback) {
getNextAppointment('provider', startDate, function(data) {
//if provider has schedule
if(!$.isEmptyObject(data.AllProviders)) {
//set provider params
//nextAppointment = data.x
//callback
if(typeof callback === 'function') {
return callback(nextAppointment); //callback from call on page load
}
} else {
if(data.ErrorCode !== 'StartDateTooFarInFuture') {
/*---------->
* this is where we this function calls itself again;
* but when it hits the callback above (provider
* has schedule), or when it hits the callback
* below (last group of appointments), it
* should run the initial callback to
* execute otherFunction()
<----------*/
getProvidersNextAppointment(data.LatestDate);
} else { //hit last group of appointments
if(typeof callback === 'function') {
return callback(null); //callback from call on page load
}
}
}
});
}
Я не включил getNextAppointment()
функцию, потому что она не имеет отношения к вопросу.Просто знайте, что он вызывает API, который возвращает информацию о встрече, а также свойство LatestDate
, которое мы используем в качестве startDate
для следующего вызова API.Мы также ищем ответ для свойства ErrorCode
, в котором говорится, что это конец результатов, чтобы мы не зацикливались вечно.