admin管理员组文章数量:1278910
I'm trying to give a delay to axios in a loop array(Vue project).
let promises = [];
for (const item of this.itemsWithIndex) {
const cmd = "od_kioskPaperUpdate";
promises.push(
await axios({
url: 'url',
method: 'post',
data: data
}).then((res) => {
if (res.statusText == 'OK') {
console.log('success', res)
} else {
console.log("failed", res)
}
})
.catch((err) => {
console.log(err)
})
)}
Promise.all(promises)
.then(async (r) => {
console.log(r)
}
How can I call this each axios function per 3 seconds?
I'm trying to give a delay to axios in a loop array(Vue project).
let promises = [];
for (const item of this.itemsWithIndex) {
const cmd = "od_kioskPaperUpdate";
promises.push(
await axios({
url: 'url',
method: 'post',
data: data
}).then((res) => {
if (res.statusText == 'OK') {
console.log('success', res)
} else {
console.log("failed", res)
}
})
.catch((err) => {
console.log(err)
})
)}
Promise.all(promises)
.then(async (r) => {
console.log(r)
}
How can I call this each axios function per 3 seconds?
Share Improve this question asked Apr 9, 2020 at 8:41 Sehun ParkSehun Park 1431 gold badge2 silver badges11 bronze badges3 Answers
Reset to default 5I solved my problem with following code. It works for me.
const axios = require('axios');
let promises = [];
const itemsWithIndex = ['a','b','c','d']
function wait(ms) {
return new Promise( (resolve) => {setTimeout(resolve, ms)});
}
const axiosFunc = async () => {
for (const item of itemsWithIndex) {
console.log('before axios')
axios({
url: 'url',
method: 'post',
data: item
}).then( await wait(5000))
}
};
axiosFunc()
Your code is not effective but you can use this where your axios request
setTimeout(() => {
console.log('axios request')
}, 3000)
try this it will work for you
let promises = [];
for (const item of this.itemsWithIndex) {
const cmd = "od_kioskPaperUpdate";
promises.push(
await axios({
url: 'url',
method: 'post',
data: data
}).then((res) => {
if (res.statusText == 'OK') {
console.log('success', res)
} else {
console.log("failed", res)
}
})
.catch((err) => {
console.log(err)
})
)
/* You can use Timeout */
setTimeout(() => {
console.log("go_to_sleep")
}, 5000);
}
Promise.all(promises)
.then(async (r) => {
console.log(r)
})
本文标签: javascriptHow to give a delay to axios in a loop arrayStack Overflow
版权声明:本文标题:javascript - How to give a delay to axios in a loop array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741271225a2369335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论