回调到异步 - 卡住了

时间:2018-01-01 13:33:36

标签: node.js asynchronous callback

这是我的工作代码:

function test(){
try {
    var TXs = [];
    var Array_Pending_API = [0]

    API.getAccount(API_Account_BTC, function(err, account) {
        account.getTransactions(null,function(err, data, pagination) {
        TXs = TXs.concat(data);     
            account.getTransactions(pagination, function(err, data, pagination) {       
            TXs = TXs.concat(data);
                account.getTransactions(pagination, function(err, data, pagination) {       
                TXs = TXs.concat(data); 

                    for (i = 0; i < TXs.length; i++) {
                        if(TXs[i].type == "send" && TXs[i].amount.amount < 0 && TXs[i].network.status != "confirmed"){ 
                            Array_Pending_API.push(-Number(TXs[i].amount.amount));
                        }
                    }

                    Pending_API = Array_Pending_API.reduce(getSum);
                })
            })
        })
    })
} catch(err) {
    console.log("Failed to get transactions")
    Pending_API = 0
}
}

我想让它异步,但我完全陷入困境。 我试过了:

async function Test(){
    var TXs = [];
    var Array_Pending_API = [0]

    const Account = await API.getAccount(API_Account_BTC)
    const TX1 = await Account.account.getTransactions(null)
    const TX2 = await Account.account.getTransactions(TX1.pagination)
    const TX3 = await Account.account.getTransactions(TX2.pagination)
    TXs = TXs.concat(TX1.data, TX2.data, TX3.data); 

    for (i = 0; i < TXs.length; i++) {
        if(TXs[i].type == "send" && TXs[i].amount.amount < 0 && TXs[i].network.status != "confirmed"){ 
            Array_Pending_API.push(-Number(TXs[i].amount.amount));
        }
    }
    Pending_API = Array_Pending_API.reduce(getSum);
}

但它根本不起作用。 错误讯息:

(node:3856) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 1): TypeError: Cannot read property 'account' of undefined
(node:3856) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

C:\Trader\Logic\node_modules\Coinbase\lib\errorHandler.js:60
if (!callback) {throw "no callback - check method signature";}
              ^
no callback - check method signature

有人能否解释一下这件事? 我不知道为什么它不起作用,并且真的想了解更多关于异步的信息。

0 个答案:

没有答案