Tian Qin
Tian Qin

Reputation: 183

promise.then() not working when I send in another promise into it

I have 2 function getAccountInfo() and getAdjustmentsInfo(accountInfo) they both return a new promise. The only different is that the second function needs the information returned from the first function.

I tried to declare these 2 function first, and call them one by one using then(). But it is not working as I expected.

These are the functions, as you can see the second function needs account_code from the first accountInfo object:

function getAccountInfo() {
    return new Promise((resolve, reject) => {
        getAccountCallbackFunc((errResponse, response) => {
            if (errResponse) {
                return reject(errResponse);
            }
            resolve(response);
        });
    });
}

function getAdjustmentsInfo(accountInfo) {
    return new Promise((resolve, reject) => {
        getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
            if (errResponse) {
                reject(errResponse);
            }
            if (response) {
                resolve(response);
            }
        });
    });
}

This is the controller code to call the functions:

var accountInfo = {};

getAccountInfo()
    .then(response => {
        accountInfo = response.data.accounts.account;
        console.log(accountInfo);
    })
    .then(getAdjustmentsInfo(accountInfo))
    .catch(err => console.log(err));

So I run the getAccountInfo() function first and run the first then() to saved the account information to the external variable accountInfo. Next I run the second then() trying to pass the accountInfo to the second function, which is not working, the second function never got called. Is that something I am missing? Please let me know.

Upvotes: 0

Views: 87

Answers (4)

ANUPAM CHAUDHARY
ANUPAM CHAUDHARY

Reputation: 151

Try this, I hope it helps.

    function getAccountInfo() {
        return new Promise((resolve, reject) => {
            getAccountCallbackFunc((errResponse, response) => {
                if (errResponse) {
                    return reject(errResponse);
                }
                resolve(response);
            });
        });
    }

    function getAdjustmentsInfo(accountInfo) {
            getAdjustmentCallbackFunc(accountInfo[0].account_code, function (errResponse, response) {
                if (errResponse) {
                    return console.log(errResponse);
                }
                    resolve(response);
            });
    }

    var promise = new Promise(function(resolve, reject){
        getAccountInfo()
        .then(response => {
            resolve(response.data.accounts.account);
            console.log(accountInfo);
        })
    }).then(function(data){
        getAdjustmentsInfo(data);
    }).catch(function(err){console.log(err)});

Upvotes: 1

Apoorv
Apoorv

Reputation: 620

There are couple of mistakes in your code. First is that you are not returning anything nor resolving any promise in the line accountInfo = response.data.accounts.account;console.log(accountInfo); so the .then(getAdjustmentsInfo(accountInfo)) will not be called. Secondly, i suppose the first argument to a .then() is always a callback with the first argument being something that is returned from the previous Promise.

Your function getAccountInfo() returns a promise. When that promise is resolved you can directly use that as follows.

var accountInfo = {};

getAccountInfo()
     .then(response => {
          accountInfo = response.data.accounts.account;
          console.log(accountInfo);
          getAdjustmentsInfo(accountInfo)
      })
     .then(resultFromPreviousPromise => {
       //Use the resultFromPreviousPromise if wanted else you can skip this .then()
      })
     .catch(err => console.log(err));

Upvotes: 1

Mohrn
Mohrn

Reputation: 816

You call getAdjustmentsInfo straight away instead of waiting for getAccountInfo and you don't return a Promise in the first then. I think this is what you mean to do:


getAccountInfo()
    .then(response => {
        accountInfo = response.data.accounts.account;
        return getAdjustmentsInfo(accountInfo)
    })
    .catch(err => console.log(err));

Upvotes: 1

TbWill4321
TbWill4321

Reputation: 8676

You are evaluating the getAdjustmentsInfo method immediately, since it's not in a callback. Try:

var accountInfo = {};

getAccountInfo()
    .then(response => {
        accountInfo = response.data.accounts.account;
        console.log(accountInfo);
    })
    .then(() => getAdjustmentsInfo(accountInfo))
    .catch(err => console.log(err));

Or even better:

var accountInfo = getAccountInfo()
    .then(response => {
        console.log(response.data.accounts.account);
        return response.data.accounts.account;
    })
    .then(account => getAdjustmentsInfo(account))
    .catch(err => {
        console.log(err));
        return {};
    })

Upvotes: 1

Related Questions