web3js订阅日志快速以供javaScript处理

发布于 2025-01-21 18:13:18 字数 708 浏览 0 评论 0原文

我正在使用web3js订阅日志,我听了交换事件,问题是.on(数据)在提供数据JavaScript无法跟上的过程中如此迅速。假设我添加了一个变量让计数= 0;每次获得新的日志时,我都会增加数字++计数,有时日志得出如此之快,以至于我会得到一个双重数字。

真正的问题是,我需要它的确切顺序,这就是为什么我将每个日志的数字列出,但这不起作用。

我如何确保我从井井有条的日志事件中获得的每个数据项? 我试图在一个简单的测试中创建一个承诺序列

let sequence = Promise.resolve();
let count = 0;

web3.eth.subscribe('logs', {
 fromBlock: block,
 topics: [
  [swapEvent]
 ]
}).on('data', (logData)=>{
 sequence = sequence.then(()=>{
  ++count
  processData(logData)
 })
});

function processData(){
 return new Promise(resolve=>{
  // do some stuff
  resolve();
 })
};

,并随机时间和随机时间解决此问题,但是在带有套接字的实际代码中,它不会保留订单。 任何人都知道如何使套筒数据保持顺序保存和一个一个一个处理?

I am using web3js to subscribe to logs, I listening to swap events, the problem is that the .on(data) is so fast in giving data JavaScript can not keep up. lets say I add a variable let count = 0; each time I get a new log I increase the number ++count, sometimes the logs come so fast I get a double number.

The real problem is I need it to be in the exact order as it is coming in, that's why I give the number to each log, but that does not work.

How would I make sure that each data item I get from the log events that they are in order?
I tried to create a promise sequence

let sequence = Promise.resolve();
let count = 0;

web3.eth.subscribe('logs', {
 fromBlock: block,
 topics: [
  [swapEvent]
 ]
}).on('data', (logData)=>{
 sequence = sequence.then(()=>{
  ++count
  processData(logData)
 })
});

function processData(){
 return new Promise(resolve=>{
  // do some stuff
  resolve();
 })
};

In a simple test with a loop and random time to resolve this works fine, but in the actual code with socket it does not keep the order.
Anyone has some idea how I can make the socket data keep in order and process one by one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2025-01-28 18:13:18

不知道为什么,但是我的问题得到了解决。

sequence = sequence.then(()=>processData(logData))

之前。

sequence = sequence.then(()=>{
  processData(logData)
})

在现在它按顺序进行

Not sure why but my problem got solved with this.

sequence = sequence.then(()=>processData(logData))

before it was

sequence = sequence.then(()=>{
  processData(logData)
})

Now its doing all in sequence.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文