kossmos
12/17/2016 - 3:25 AM

Как правильно организовать сложную цепочку ajax-запросов?

Как правильно организовать сложную цепочку ajax-запросов?

this.start = function(){
    // останавливаем репликацию
    return this.stopAndClearExistsReplication()
        // удаляем локальную БД
        .always(function(){ return this.deleteLocalDB() })
        // создаём локальную БД
        .always(function(){ return this.createLocalDB() })
        // ставим БД на репликацию
        .done(function(){ return this.addReplicationDocument() })
        // ставим репликацию на отслеживание
        .done(function(){ return this.replicationMonitoring() });
};

this.start = function(){
    // останавливаем репликацию
    return this.stopAndClearExistsReplication()
        // удаляем локальную БД
        .always(deleteLocalDB)
        // создаём локальную БД
        .always(createLocalDB)
        // ставим БД на репликацию
        .done(addReplicationDocument)
        // ставим репликацию на отслеживание
        .done();
};

function deleteLocalDB(){ return this.deleteLocalDB() }
function createLocalDB(){ return this.createLocalDB() }
function addReplicationDocument(){ return this.addReplicationDocument() }
function replicationMonitoring(){ return this.replicationMonitoring() }

/**
 * Если доступны стрелочные функции и this один на все методы
 */
this.start = function(){
    // останавливаем репликацию
    return this.stopAndClearExistsReplication()
        // удаляем локальную БД
        .always(() => this.deleteLocalDB())
        // создаём локальную БД
        .always(() =>  this.createLocalDB())
        // ставим БД на репликацию
        .done(() => this.addReplicationDocument())
        // ставим репликацию на отслеживание
        .done(() =>  this.replicationMonitoring());
};