簡體   English   中英

當我向其中發送另一個諾言時,promise.then()無法正常工作

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

我有2個函數getAccountInfo()和getAdjustmentsInfo(accountInfo),它們都返回一個新的Promise。 唯一的不同是第二個功能需要從第一個功能返回的信息。

我嘗試先聲明這兩個函數,然后使用then()逐個調用它們。 但是它沒有按我預期的那樣工作。

這些是函數,如您所見,第二個函數需要第一個accountInfo對象的account_code:

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);
            }
        });
    });
}

這是調用函數的控制器代碼:

var accountInfo = {};

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

因此,我先運行getAccountInfo()函數,然后先運行then(),以將帳戶信息保存到外部變量accountInfo。 接下來,我運行第二個then()嘗試將accountInfo傳遞給第二個功能,該功能不起作用,第二個功能從未被調用。 那是我所缺少的嗎? 請告訴我。

您正在立即評估getAdjustmentsInfo方法,因為它不在回調中。 嘗試:

var accountInfo = {};

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

甚至更好:

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 {};
    })

你叫getAdjustmentsInfo直線距離,而不是等待getAccountInfo ,你不返回Promise第一then 我認為這是您的意思:


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

您的代碼中有幾個錯誤。 首先,您不會在accountInfo = response.data.accounts.account;console.log(accountInfo);行中返回任何內容或解決任何承諾accountInfo = response.data.accounts.account;console.log(accountInfo); 因此.then(getAdjustmentsInfo(accountInfo))不會被調用。 其次,我假設.then()的第一個參數始終是一個回調,第一個參數是從上一個Promise返回的東西。

您的函數getAccountInfo()返回一個promise。 兌現承諾后,您可以按以下方式直接使用它。

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));

試試這個,希望對您有所幫助。

    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)});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM