gpt4 book ai didi

javascript - 通过收集迭代时的 meteor 延迟

转载 作者:行者123 更新时间:2023-12-03 06:22:29 24 4
gpt4 key购买 nike

您好,当我迭代集合时,我想在我的 meteor 服务器端方法中构建一个延迟。Meteor._sleepForMs 方法会导致异常并每次重新启动我的应用程序。有哪些方法可以延迟集合迭代?

 Meteor.methods({
....
start: function() {
let data = Strategy.find({}, {limit: 5}).fetch();
for (let i = 0; i < data.length; ++i) {
mqttClient.publish("test", data[i].charge);
Meteor._sleepForMs(data[i].duration*1000);
}
}

最佳答案

使用Meteor.setTimeout :

start: function() {
Meteor.setTimeout(function() {
let data = Strategy.find({}, {limit: 5}).fetch();
for (let i = 0; i < data.length; ++i) {
mqttClient.publish("test", data[i].charge);
}
}, 1000);
}

另请参阅why to use that vs vanilla setTimeout() .

编辑对于变量:

start: function() {
let data = Strategy.find({}, {limit: 5}).fetch();
for (let i = 0; i < data.length; ++i) {
Meteor.setTimeout(function() {
mqttClient.publish("test", data[i].charge);
}, data[i].duration);
}
}

编辑您可以对 RawCollection 对象执行您想要的操作,将其 maxTimeMS 设置为

var rawCollection = Strategy.rawCollection();
// Number.MAX_SAFE_INTEGER should be sufficient time
var cursor = rawCollection.find({}).maxTimeMS(Number.MAX_SAFE_INTEGER );
var myData = fetchCursor(cursor);

var fetchCursor = Meteor.wrapAsync(function
fetchCursor (cursor, cb) {
cursor.each(function (err, doc) {
if (err) return cb(err);
if (!doc) return cb(null, { done: true }); // no more documents

// use doc here.
});
});

关于javascript - 通过收集迭代时的 meteor 延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com