有效地循环多个承诺
Loop though several promises efficiently
我是 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 个)。哪种编程方式最好?
您尝试过使用 Promise.all 吗?
我认为是这样工作的:
Promise.all([asyncFunc1(), asyncFunc2(), asyncFunc3(),... ])
.then(function(result){...})
.catch(function(error){...});
当所有的承诺都已解决时调用 .then()。
你可以的。
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
然后部分将是解析值的数组
我是 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 个)。哪种编程方式最好?
您尝试过使用 Promise.all 吗? 我认为是这样工作的:
Promise.all([asyncFunc1(), asyncFunc2(), asyncFunc3(),... ])
.then(function(result){...})
.catch(function(error){...});
当所有的承诺都已解决时调用 .then()。
你可以的。
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
然后部分将是解析值的数组