Javascript 在循环的每次迭代中调用Process.on函数
我有两个问题,Javascript 在循环的每次迭代中调用Process.on函数,javascript,node.js,unhandled-exception,Javascript,Node.js,Unhandled Exception,我有两个问题, 我的循环将运行三次,并产生一次错误(unhandledRejection),第二次和第三次没有错误。但是结果表明,process.on在每次迭代中都被调用(意味着三次)。为什么?仅当发生未经处理的弹出错误时才应调用process.on,而不应为其他人调用???我的代码有什么问题 如果我想在process.on函数中访问我的'tx'成员变量(即tx.n、tx.to等),并想保存它们…,我该怎么做 这是我的密码 const abcWeb = require('abc-web'); c
const abcWeb = require('abc-web');
const abcWeb = new abcWeb('http://testnet-jsonrpc.abc-xyz.org:12537');
const abTx = require('abc-transaction');
var n = 12;
var gp = 10;
var gl = 21000;
var to = '5989d50787787b7e7';
var value = 70;
var limit = 3;
var i=1;
for (i = 0; i < limit; i++) {
try{
const tx = new abTx({
n: n,
gp: gp,
gl:gl ,
to: to,
value:value ,
});
abTx.sign(Buffer.from('8f2016c58e898238dd5b4e00', 'hex'));
abcWeb.abx.sendSignedTransaction('0x' + tx.serialize().toString('hex'));
console.log(abTx);
} catch (exception) {
var message = exception.message;
console.log(message);
}
// n +=1;
}
process.on('unhandledRejection', error => {
console.log("Rejected due to ",error)
})
const abcWeb=require('abc-web');
const abcWeb=新的abcWeb('http://testnet-jsonrpc.abc-xyz.org:12537');
const abTx=require('abc-transaction');
var n=12;
var-gp=10;
var gl=21000;
var to='5989d50787787b7e7';
var值=70;
var限值=3;
var i=1;
对于(i=0;i{
console.log(“由于”,错误而被拒绝)
})
输出为
由于错误而被拒绝:节点错误:{“代码”:
由于错误而被拒绝:节点错误:{“代码”:
由于错误而被拒绝:节点错误:{“代码”:
首先,在循环中使用try…catch将为您提供更多参考外观的意外结果 现在,对于您的第一个查询,您应该尝试以下hack以获得预期结果 只需将您的try…catch块放入函数中并调用它即可
for (i = 0; i < limit; i++) {
const tx = new abTx({
n: n,
gp: gp,
gl:gl ,
to: to,
value:value ,
});
myTryCatch(tx, abTx, abcWeb);
}
function myTryCatch(tx, abTx, abcWeb){
try{
abTx.sign(Buffer.from('8f2016c58e898238dd5b4e00', 'hex'));
abcWeb.abx.sendSignedTransaction('0x' + tx.serialize().toString('hex'));
console.log(abTx);
} catch (exception) {
var message = exception.message;
console.log(message);
}
}
(i=0;i{
const tx=新的abTx({
n:n,
gp:gp,
德国劳埃德船级社:德国劳埃德船级社,
致:致,,
价值:价值,
});
myTryCatch(tx、abTx、abcWeb);
}
功能myTryCatch(tx、abTx、abcWeb){
试一试{
abTx.符号(缓冲区起始('8f2016c58e898238dd5b4e00','hex');
abcWeb.abx.sendSignedTransaction('0x'+tx.serialize().toString('hex');
控制台日志(abTx);
}捕获(例外){
var message=exception.message;
控制台日志(消息);
}
}