- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 NodeJS 应用程序,它从不同的 API 获取大量信息。
在最大的抓取过程中,我的 promise 出现了一些问题。
我想我发起的请求太多而且速度太快......
var promises = [];
list_consumptions.forEach(function (item)
{
item.consumptions.forEach(function (data)
{
promises.push(getDetails(item.line, data));
});
});
在 getDetails() 中
function getDetails(line, item)
{
var deferred = Q.defer();
var result = [];
ovh.request('GET', '/telephony/*******/service/' + line + '/voiceConsumption/' + item, function (err, fetched_data)
{
if (!err)
{
result = {
'line': line,
'consumption_id': item,
'type': fetched_data.wayType,
'calling': fetched_data.calling,
'called': fetched_data.called,
'plan_type': fetched_data.planType,
'destination_type': fetched_data.destinationType,
'date': fetched_data.creationDatetime,
'duration': fetched_data.duration,
'price': fetched_data.priceWithoutTax
};
// console.log("RESULT: ",result);
deferred.resolve(result);
}
else
{
deferred.reject(err);
console.log(err);
}
});
return deferred.promise;
}
循环之后:
Q.allSettled(promises).done(function(final_result)
{
final_result.forEach(function (promise_fetch){
if (promise_fetch.state != 'fulfilled')
{
console.log("ERREUR Merge");
}
else
{
all_consumptions.push(promise_fetch.value);
}
deferred.resolve(all_consumptions);
});
return deferred.promise;
});
使用这段代码,我得到了专门的日志错误:400
我会尝试使用一些 setTimeOut 来减慢我的循环,在这种情况下,获取成功,但我的 q.allSettled 被跳过...我真的迷路了...
有什么想法可以改进我的 promise 句柄/循环句柄吗?我很确定你已经知道了,但这是我学习 JS 和 NodeJS 的第一周。
非常感谢您的帮助...
最佳答案
您可以使用 promise 循环,如下所示:
function pwhile(condition, body) {
var done = Q.defer();
function loop() {
if (!condition())
return done.resolve();
Q.when(body(), loop, done.reject);
}
Q.nextTick(loop);
return done.promise;
}
然后,您的代码将是:
list_consumptions.forEach(function (item) {
var i = 0;
pwhile(function() { return i < item.consumptions.length; }, function() {
i++;
return getDetails(item.line, data)
.then(function(res) {
all_consumptions.push(res);
})
.catch(function(reason) {
console.log("ERREUR Merge");
});
}).then(function() {
// do something with all_consumptions here
});
});
关于javascript - q.allSettled 有默认超时吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32827026/
例子一 function first(a, b) { return a + b; } function second() { return Math.floor(Math.sqrt(a
我想证明或证伪forall (P Q : Prop), (P -> Q) -> (Q -> P) -> P = Q.在柯克。这是我的方法。 Inductive True2 : Prop := | O
程序提取数字,我希望它继续循环,直到用户键入“Q”/“q”键。例如,当用户按下“O”键时,程序应打印他们输入的数字的个位数,对于用户输入的任何 3 位数字,依此类推。当我现在运行代码时,没有输出,但也
我收到以下代码的警告,我不明白为什么。 类似于这个问题:Unhandled rejection reasons (should be empty) 但是…… 我很确定我正在处理所有错误,那么为什么会出
我正在使用 Express 构建一个博客站点,并且是第一次使用 Q,我希望能够利用资深 Q 用户的知识。 我向我的数据库发出一个请求以加载帖子数据,另一个请求命中 Instagram API(除非它已
我刚刚找到有关 apache solr 的信息,并且在我成功安装了带有 Tomcat 的 apache Solr 之后。然后我开始使用 Apache Solr 进行搜索。 但我对 Apache Sol
我在 Stack Overflow post 上看到了下图 但是,我对“p OR q”、“p AND q”的结果感到困惑,其中“p”等于“false”,“q”等于“unknown”。 在图中,“p O
有人向我提出了这个问题。 n = 77 n = p*q p and q is a prime number 用蛮力找到p和q。 到目前为止我的代码: public class If { pub
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 4 年前。 Improve
我注意到如果 .then()回调由于任何错误(例如对 undefined variable 的操作)而失败,并且没有 .catch()相关联,该错误将被静默忽略。 这在开发过程中很不舒服。 例如,此代
以下示例打印“SAME”: if (q/\\a/ eq q/\a/) { print "SAME\n"; } else { print "DIFFERENT\n"; } 我理解这与 d
我画了 qq 图多元回归,得到了下面的图。谁能告诉我为什么红线下面有两个点?这些点对我的模型有影响吗? 我使用下面的代码来绘制图表。 from sklearn.linear_model import
我确定 int q[6][4] 中的 q 的类型为 (**q)[4],即, 指向大小为 4 的整数数组的指针。但是我的书(我发现它很可疑!!)说函数定义中的 int q[][4] 部分 void fo
我试图用 tatics [intros]、[apply]、[assumption]、[destruct]、[left]、[right]、[split] 来证明这个引理,但失败了。谁能教教我怎么证明?
使用 q.all 时,我的数组中的立即函数似乎没有执行。每个函数都应该创建一个已解决的 promise ,将其打印到控制台,然后返回它。我没有看到控制台输出,但 Q.all 似乎很满意,并用 2 个空
我想在 OpenAI 的 Bipedal Walker v2 中实现 Q 学习,但在寻找教程后,它们似乎总是有限环境,这使得 Q 矩阵和奖励矩阵易于初始化。 例如:http://mnemstudio.
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在创建一个基于 AngularJS、Breeze 和 RequireJS 的单页应用程序。在使用 requirejs 设置 AMD 以使用 Angular 和 Breeze 时,我遇到了 Bree
这个问题在这里已经有了答案: Difference between defer().promise and Promise (1 个回答) 关闭 6 年前。 在 Angular 中,以下代码片段似乎
我写了一个 tcp 服务器和一个 tcp 客户端,客户端只向服务器发送数据并打印它写入了多少字节,服务器只接受连接,然后我使用 netstat 显示套接字的 Recv-Q 和 Send-问,我发现 R
我是一名优秀的程序员,十分优秀!