Why nested promises are not working as expected?

can someone try to fix my problem?

I am trying to get this output:

100x do C
done B
done A

But everytime i get this one:

10x do C
done B
done A

Is it possible to get it without using async/await feature?

function a() {
    let myPromise = new Promise(function(myResolve, myReject) {
        b();
        myResolve()
    });

    myPromise.then(function() {
        console.log('done A')
    });
}

function b() {
    let myPromise = new Promise(function(myResolve, myReject) {
        for (i = 0; i < 10; i++) {
            c();
        }
    myResolve()
    });
    
    myPromise.then(function() {
    console.log('done B')
    });
}

function c() {
    for (i = 0; i < 10; i++) {
      console.log('do C')
    }
}

a()

21 thoughts on “Why nested promises are not working as expected?”

Leave a Comment