Process.on 在循环的每次迭代中调用的函数
Process.on function called on every iteration of loop
我有以下两个问题;
我的循环将 运行 三次,一次产生错误 (unhandledRejection),第二次和第三次没有错误。但结果显示 process.on 调用了每次迭代(意味着三次)。为什么 ? process.on 应该只在发生 unhandledRejection 错误时被调用,而不是其他人 ???我的代码有什么问题?
如果我想在process.on函数中访问我的'tx'成员变量(即tx.n、tx.to等)并且想要拯救他们……,我怎么能那样做?
这是我的代码
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)
})
输出为
Rejected due to Error: Node error: {"code":
Rejected due to Error: Node error: {"code":
Rejected due to Error: Node error: {"code":
首先,在循环中使用 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);
}
}
我有以下两个问题;
我的循环将 运行 三次,一次产生错误 (unhandledRejection),第二次和第三次没有错误。但结果显示 process.on 调用了每次迭代(意味着三次)。为什么 ? process.on 应该只在发生 unhandledRejection 错误时被调用,而不是其他人 ???我的代码有什么问题?
如果我想在process.on函数中访问我的'tx'成员变量(即tx.n、tx.to等)并且想要拯救他们……,我怎么能那样做?
这是我的代码
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)
})
输出为
Rejected due to Error: Node error: {"code": Rejected due to Error: Node error: {"code": Rejected due to Error: Node error: {"code":
首先,在循环中使用 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);
}
}