有效地循环几个承诺

时间:2017-10-29 23:37:00

标签: javascript node.js performance promise hyperledger-fabric

我是Node.js和异步调用的新手,但我正在尝试构建一个可以自动进行多个事务的程序。问题是,现在我首先连接到Hyperledger Fabric,然后通过for循环运行该函数。

这是相当快的,但我希望大大提高速度。这是启动连接的代码:

init() {
        return this.businessNetworkConnection.connect(this.connectionProfile, this.businessNetworkIdentifier, participantId, participantPwd)
            .then((result) => {
                console.log(chalk.green('Connected to Hyperledger!'));
                this.businessNetworkDefinition = result;
            })
            .catch(function (error) {
                console.log('An error occured: ', chalk.bold.red(error));
            });
    }

这是允许我在分类帐上进行交易的代码:

makeTransaction(fromID, toID, funds) {
        const METHOD = 'makeTransaction';
        let from;
        let walletRegistry;
        let to;

        return this.businessNetworkConnection.getAssetRegistry('org.acme.Wallet')
            .then((registry) => {
                console.log(1);
                walletRegistry = registry;
                return walletRegistry.get(fromID);
            })
            .then((fromm) => {
                console.log(2);
                from = fromm;
                return walletRegistry.get(toID);
            })
            .then((too) => {
                to = too;
            })
            .then(() => {
                let serializer = this.businessNetworkDefinition.getSerializer();
                let resource = serializer.fromJSON({
                    "$class": "org.acme.Transfer",
                    "amount": funds,
                    "from": {
                        "$class": "org.acme.Wallet",
                        "id": from.getIdentifier(),
                        "balance": from.balance,
                        "owner": "resource:org.acme.Client#" + from.owner.getIdentifier()
                    },
                    "to": {
                        "$class": "org.acme.Wallet",
                        "id": to.getIdentifier(),
                        "balance": to.balance,
                        "owner": "resource:org.acme.Client#" + to.owner.getIdentifier()
                    }
                });

                return this.businessNetworkConnection.submitTransaction(resource);
            })
            .catch(function (error) {
                throw (error);
            })
    }

但是现在使交易发生的功能看起来像这样。

static transfer(fromID, toID, funds) {
        let bm = new BlockchainManager();
        return bm.init()
            .then(() => {
                return bm.makeTransaction(fromID, toID, funds);
            })
            .then(() => {
                console.log('Success!');
            })
            .catch(function (error) {
                console.log('An error occured: ', chalk.bold.red(error));
                process.exit(1);
            });
    }

我不认为这是进行大量交易的最佳方式(我希望在某些时候每秒运行1000次以上)。哪个是编程的最好方法?

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用Promise.all? 我认为是这样的:

Promise.all([asyncFunc1(), asyncFunc2(), asyncFunc3(),... ])
.then(function(result){...})
.catch(function(error){...});

当所有承诺都已解决时,会调用.then()。

答案 1 :(得分:0)

你可以做到。

let all_promise = [];
/*
* your loop here
* for(let i = 0; i < am ; I++){
*    all_promise.push(transfer(i,i+1,i*29);
*  } 
*/
Promise.all(all_promise).then(arr => {
      // arr is the map arr of all the promises that "transfer()" method returned
      // you can Iterate the arr since its the resolve value of all the promises that was push from all_promise . you need to refactor big time in your code.
})

我只是注释掉for循环让你有个主意.. 这个想法是你将尚未解析的所有承诺推送到数组然后将该数组传递给Promise.all然后部分将是解析值的数组