gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:42:57 26 4
gpt4 key购买 nike

我正在使用

$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/

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