web3js订阅日志快速以供javaScript处理
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么,但是我的问题得到了解决。
之前。
在现在它按顺序进行
Not sure why but my problem got solved with this.
before it was
Now its doing all in sequence.