【发布时间】:2022-01-28 01:18:45
【问题描述】:
这是我第一次使用 Promise,所以我敢肯定这里有一些非常愚蠢的错误。我想要做的是发送一个在 for 循环中的 http 请求。
在没有承诺的情况下执行此操作时,我可以正常运行此循环并且一切正常。但是,当我使用 promise 执行此操作时,它只返回一个对象(应该是多个,因为它发送多个请求)
这是我的代码
function run(o,initialvalue){
const test = new Promise( (resolve,reject) => {
const collectionCountUrl = 'https://x.io/rpc/Query?q=%7B%22%24match%22%3A%7B%22collectionSymbol%22%3A%22'+o.name+'%22%7D%2C%22%24sort%22%3A%7B%22takerAmount%22%3A1%2C%22createdAt%22%3A-1%7D%2C%22%24skip%22%3A'+initialvalue+'%2C%22%24limit%22%3A500%7D'
promises = []
for(i=0; i < o.count(/*in this case 60, so 3 requests*/); i+= 20){
$.get(collectionCountUrl).success(resolve).fail(reject) // This should be sending multiple of the requests above, correct? ^
}
})
test.then(function(data){
console.log(data) // this should return the data from each request? im not sure
})
}
我尝试查看这篇文章,看看我是否设置了错误的 for 循环,但我不认为我是。 Promise for-loop with Ajax requests
【问题讨论】:
标签: javascript