0

My Program Structure is Like this

 function a(){
    b().then(function(res){
    })
    f().then(function(res){
    })
    }

    and then function c(){
    d().then(function(res){
    });
    e().then(function(res){

    })
    };

    function b(){
    //async operation
    }
    function f(){
    //async operation
    }
    function e(){
    //async operation
    }
    function d(){
    //async operation
    }

What i want to achieve is that when every function inside b is done , then c must start working , So Basically I need a little help in structuring my program but i cannot change much of my inside function as I have written a lot of them and it's working with timeout but I know it's a very poor workaround but if you can suggest how to do it with minimal modification it would be helpful and even if I have to modify it what will be the best approach to do so ? , So I can prevent mistakes in future.

I finally solved the problem by making proper use of promise.push and then resolving all the promises in $q.all(promise)

function a() {
    var deferred = $q.defer();
    deferred.resolve('a')
    return deferred.promise;
}

function b() {
    var deferred = $q.defer();
    deferred.resolve('b')
    return deferred.promise;
}

function a() {
    var deferred = $q.defer();
    deferred.resolve('a')
    return deferred.promise;
}

function b() {
    var deferred = $q.defer();
    deferred.resolve('b')
    return deferred.promise;
}

function g() {
    var deferred = $q.defer();
    deferred.resolve('a')
    return deferred.promise;
}

function h() {
    var deferred = $q.defer();
    deferred.resolve('b')
    return deferred.promise;
}


function c() {
    var deferred = $q.defer();
    var promise = [];
    promise.push(a().then(function(res) {
        return res
    }));
    promise.push(b().then(function(res) {
        return res;
    })) $q.all(promise).then(function(res) {
        deferred.resolve(res);
    }) return deferred.promise;
}

function e() {
    var deferred = $q.defer();
    var promise = [];
    promise.push(g().then(function(res) {
        return res
    })) promise.push(h().then(function(res) {
        return res;
    })) $q.all(promise).then(function(res) {
        deferred.resolve(res);
    }) return deferred.promise;

}

function final() {
    var deferred = $q.defer();
    var promise = [];
    promise.push(c().then(function(res) {
        return res
    })) promise.push(e().then(function(res) {
        return res;
    })) $q.all(promise).then(function(res) {
        deferred.resolve(res);
    }) return deferred.promise;
}

final().then(function(res) {
    console.log('move next')
})
Sagar
  • 475
  • 2
  • 8
  • `I know about promises` - promises don't make asynchronous code synchronous - nothing can do that if you really think about it – Jaromanda X Jun 18 '17 at 05:10
  • Yes I know it will not make async sync , but it can atleast make the async calls wait for something , like I say I can write a function a(){ var q = $q.defer(); q.resolve('success'); return q.promise;} and then a function c(){ //a.then(function(res){console.log('hello'+res)})} so effectively my function c is going to wait for a to resolve , similary i have lot of async calls but i want that finally when each and every function inside my bigger function has completed it must return an object and then I can proceed with other operation – Sagar Jun 18 '17 at 05:28
  • it's just that the title of the question asks how to make asynchronous code synchronous - did I misunderstand the title?" – Jaromanda X Jun 18 '17 at 05:33
  • Actually what i want to do is make function a and c sync and anything inside a must be performed before anything inside function c and I just want to know how to do it with minimal change in my existing code. – Sagar Jun 18 '17 at 06:04
  • 1
    `is make function a and c sync` you can't, because they contain asynchronous code, you need to use methods you're already using (.then) – Jaromanda X Jun 18 '17 at 06:08
  • Okay , But I just want to ask that if I use like function g(){ a.then(function(){c()})} but the next problem with me is that i have to be sure all the function within a have completed , how to ensure that all function inside a has run ? – Sagar Jun 18 '17 at 06:13

0 Answers0