javascript - $q.allPromises 返回一个数组,但我只想要一个元素,而不是全部

标签 javascript angularjs node.js angular-promise

我正在使用

$q.allPromises(http request).then (function (data) {
  //more code logic
})

它第一次工作,但我在 24 小时后调用此方法,发现“数据”是一个对象数组,每次我使用新的 http json 对象调用 $q.allPromises 时它都会被追加。

我怎么能忘记数组中的旧“对象”。我每 24 小时拉一个 json,只关心我刚刚获取的 json 对象。我想忽略从之前的 http promise 请求中提取的 json 对象,但它似乎一直附加到一个数组中

我尝试添加

$q.allPromises(http request).then (function (data) {
  //more code logic
  data.shift ();
})

shift() 应该从数组中删除第一个元素,但它似乎不起作用。

最佳答案

您不需要使用 $q.all$http 提供者自己返回一个 promise :

$http.get({...}).then(function(response) {
  console.log(response.data) // this will print actual data
});

$http.post({...}).then(function(response) {
  console.log(response.data) // this will print actual data
});

$q.all 是一种特殊的方法,用于等待许多 promise 在采取行动之前解决,如下所示:

var promiseA = $http.get({...}).then(function(response) {
  console.log(response.data) // this will print actual data
});

var promiseB = $http.post({...}).then(function(response) {
  console.log(response.data) // this will print actual data
});

var arrayOfPromises = $q.all([promiseA, promiseB]).then(function(arrayOfResults) {
  console.log(arrayOfResults); // this will print an array of the results of the http requests
});

关于javascript - $q.allPromises 返回一个数组,但我只想要一个元素,而不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962232/

相关文章:

javascript - 从 Controller 中选择选择框选项

angularjs - 如何禁用 ui.bootstrap.pagination

javascript - AngularJS 通过下拉列表中的选择过滤文本

node.js - Nodejs模块安装错误: Request path contains unescaped characters

javascript - 如何在桌面上模拟移动(安卓)浏览器

php - 单击时,切换图像以及 MySQL 数据库中的值

node.js - 使用 NTVS(Visual Studio 的 Node 工具)调试 Meteor 应用程序

node.js - 重写规则,以便正确加载具有绝对路径的 css 和 js 文件

javascript - 如何将 html 代码块分配给 javascript 变量(以调用 javascript 调用)

javascript - 在事件处理程序中使用 ReactDOM.render 以避免渲染昂贵的组件