事务完成引发UnhandledPromiseRejectionWarning"/>
Symbol SDK:等待事务完成引发UnhandledPromiseRejectionWarning
我正在尝试使用SymbolSdk。
我尝试获取给定“ mosaic”的帐户列表的余额。
我想让代码查询区块链并在正确读取了每个用户的余额后解决承诺。
然后,我想加入所有的Promise,并在最后显示每个查询的输出。
我从symbol documentation得到了一个代码样本,展示了当从区块链接收到信息时如何触发代码。
但是它总是抛出UnhandledPromiseRejectionWarning
,我找不到捕捉这些错误的方法。
我想它们来自accountHttp.getAccountInfo(userAddress)
函数中的某个位置,但我无法再潜水了。
import * as SymbolSdk from 'symbol-sdk';
import { merge, of, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
const nodeUrl = 'http://localhost:3000';
export async function userBalance(userAddress: SymbolSdk.Address, mosaicId: SymbolSdk.MosaicId): Promise<SymbolSdk.UInt64> {
const repositoryFactory = new SymbolSdk.RepositoryFactoryHttp(nodeUrl);
const accountHttp = repositoryFactory.createAccountRepository();
return new Promise<SymbolSdk.UInt64>(async (resolve, reject) => {
accountHttp.getAccountInfo(userAddress)
.pipe(catchError((err) => {
const next = of(err)
reject(err);
return next;
}))
.subscribe((transactions) => {
for (const mosaic of transactions.mosaics) {
// console.log(userAddress.plain(), mosaic.id.toHex(), mosaic.amount.toString());
if (mosaicId.equals(mosaic.id)) {
resolve(mosaic.amount);
}
}
resolve(new SymbolSdk.UInt64([0, 0]));
},
(err) => reject(err))
});
}
if (require.main === module) {
const promiseList = [];
const users = ["TCC4A2H6VX4MVBYHBG33JRCL7SI4Z7OKSKUQRNXS", "TBHE2SZCMVGQTDDVSMM6G7KE33IWCXA726WXEV3H", "TBKFTBO4X5EIEHE4LDWVFALJGQU3HCVTAHN2NXCH",
"TCWUJ4ENXWC4QYJ4N7ESVUHTBXL6IJ4TTPNFME3F", "TC2X5PCGSOEGX6TPZYTZQZYJBOP2QOPDC4VQ5GCK"];
const rawMosaicId = "0874BB5FEAAA53C4";
users.forEach(async (user) => {
const promise = new Promise<[string, SymbolSdk.UInt64, SymbolSdk.MosaicId]>((resolve, reject) => {
try {
const address = SymbolSdk.Address.createFromRawAddress(user);
const mosaicId = new SymbolSdk.MosaicId(rawMosaicId);
userBalance(address, mosaicId).then((amount) => {
resolve([user, amount, mosaicId]);
}).catch(reject);
}
catch (err) {
reject(err);
}
});
promiseList.push(promise);
});
Promise.allSettled(promiseList).then((values) => {
console.log("Chain over");
values.forEach((settled) => {
if (settled.status === "fulfilled")
{
const [user, amount, mosaicId] = settled.value;
console.log(`User ${user}, amount of ${mosaicId.toHex()}: ${amount.toString()}`);
} else {
console.log(`Failed to get balance, reason: ${settled.reason}`);
}
});
}).catch(console.error);
}
这是我得到的输出示例:
(node:30500) UnhandledPromiseRejectionWarning: HttpError: HTTP request failed
at Request._callback (/home/my_user/my_projects/my_symbol_project/node_modules/symbol-openapi-typescript-node-client/dist/api/nodeRoutesApi.js:168:40)
at Request.self.callback (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:185:22)
at Request.emit (events.js:315:20)
at Request.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1154:10)
at Request.emit (events.js:315:20)
at IncomingMessage.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1225:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:30500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:30500) [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.
(node:30500) UnhandledPromiseRejectionWarning: HttpError: HTTP request failed
at Request._callback (/home/my_user/my_projects/my_symbol_project/node_modules/symbol-openapi-typescript-node-client/dist/api/nodeRoutesApi.js:168:40)
at Request.self.callback (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:185:22)
at Request.emit (events.js:315:20)
at Request.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1154:10)
at Request.emit (events.js:315:20)
at IncomingMessage.<anonymous> (/home/my_user/my_projects/my_symbol_project/node_modules/request/request.js:1076:12)
at Object.onceWrapper (events.js:421:28)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (_stream_readable.js:1225:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:30500) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see .html#cli_unhandled_rejections_mode). (rejection id: 2)
Chain over
User TCC4A2H6VX4MVBYHBG33JRCL7SI4Z7OKSKUQRNXS, amount of 0874BB5FEAAA53C4: 0
User TBHE2SZCMVGQTDDVSMM6G7KE33IWCXA726WXEV3H, amount of 0874BB5FEAAA53C4: 169489
User TBKFTBO4X5EIEHE4LDWVFALJGQU3HCVTAHN2NXCH, amount of 0874BB5FEAAA53C4: 177213
User TCWUJ4ENXWC4QYJ4N7ESVUHTBXL6IJ4TTPNFME3F, amount of 0874BB5FEAAA53C4: 180560
User TC2X5PCGSOEGX6TPZYTZQZYJBOP2QOPDC4VQ5GCK, amount of 0874BB5FEAAA53C4: 6472738
回答如下:但是它不断抛出UnhandledPromiseRejectionWarning,我无法找到捕获这些错误的方法。
这里是如何导致此错误的示例
window.addEventListener("unhandledrejection", event => {
console.warn(`UNHANDLED PROMISE REJECTION: ${event.reason}`);
});
(async _ => {
throw new Error('from await/async');
})();
new Promise((resolve, reject) => reject('from promise'));
更多推荐
Symbol SDK:等待事务完成引发UnhandledPromiseRejectionWarning
发布评论