使用 setInterval 或 setTimeout 对 API 的时间承诺请求
Time promise requests to an API using setInterval or setTimeout
我有这个问题 - 我正在尝试从接受整数 ID 的常量 url 中获取数据。
我将这些整数堆叠在数组中。
我不想让服务器充满请求,所以我尝试使用 setInterval 和 setTimeout 来为请求计时。
我确实考虑到承诺可能需要一些时间才能完成,但无法弄清楚如何明确应用它。
这段代码的结果就是:
"[] 1"
const axios = require('axios')
const dataFile = require('../data/car_data')
const modelNameUrl = 'https://www.gov.il/api/mot/carlistprice/api/modelName?yazran_id='
const carId = dataFile.map(data => data.manufacturer_id)
const fetch = async (id) => {
const dataFetched = await axios.get(`${modelNameUrl}${id}`).then()
return dataFetched.data.dgamim_yazran
}
let index = 0
setInterval(async () => {
const data = await fetch(index)
index++
console.log(data, index)
}, 10000)
用于进一步调试的附加代码:
const axios = require('axios')
// const dataFile = require('../data/car_data')
// dataFile.map(data => data.manufacturer_id)
const modelNameUrl = 'https://www.gov.il/api/mot/carlistprice/api/modelName?yazran_id='
let dataArray = []
const fetch = async (id) => {
const dataFetched = await axios.get(`${modelNameUrl}${id}`)
return dataFetched.data.dgamim_yazran
}
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
let integerSource = [
6, 67, 4, 5, 9, 60, 7, 30, 107, 113, 19,
120, 15, 17, 12, 59, 3, 129, 56, 1, 124, 29,
26, 64, 33, 63, 131, 112, 2, 39, 133, 38, 40,
48, 52, 53, 54, 50, 13, 110, 51, 57, 68, 23,
44, 22, 41, 21, 10, 32, 47, 45, 11
]
async function runLoop() {
for (let index of integerSource) {
try {
const data = await fetch(index);
console.log(data, index);
await delay(5000);
} catch (e) {
console.log(`Error on index ${index}`, e);
throw new Error
}
}
}
runLoop().then(() => {
console.log("all done");
}).catch(err => {
console.log("ended with error\n", err);
});
我建议只使用 for
循环和 await
然后 promise-returning 延迟。这将 space 从您的 API 呼叫结束时开始,而不是从开始时开始。您现有的方案不会立即增加索引,因此它甚至可以重复调用 fetch()
.
你说你在一个数组中有所需的整数堆栈,但没有在你的代码中显示它。您可以使用仅递增索引的 for
循环,也可以使用 for
循环从数组中提取整数。
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
let integerSource = [...]; // your array of integer values
async function runLoop() {
for (let index of integerSource) {
try {
const data = await fetch(index);
conole.log(data, index);
await delay(10000);
} catch(e) {
conole.log(`Error on index ${index}`, e);
// decide here whether you continue with further requests
// or throw e to stop further processing
}
}
}
// run the loop here
runLoop().then(() => {
console.log("all done");
}).catch(err => {
console.log("ended with error");
});
我有这个问题 - 我正在尝试从接受整数 ID 的常量 url 中获取数据。
我将这些整数堆叠在数组中。 我不想让服务器充满请求,所以我尝试使用 setInterval 和 setTimeout 来为请求计时。
我确实考虑到承诺可能需要一些时间才能完成,但无法弄清楚如何明确应用它。
这段代码的结果就是: "[] 1"
const axios = require('axios')
const dataFile = require('../data/car_data')
const modelNameUrl = 'https://www.gov.il/api/mot/carlistprice/api/modelName?yazran_id='
const carId = dataFile.map(data => data.manufacturer_id)
const fetch = async (id) => {
const dataFetched = await axios.get(`${modelNameUrl}${id}`).then()
return dataFetched.data.dgamim_yazran
}
let index = 0
setInterval(async () => {
const data = await fetch(index)
index++
console.log(data, index)
}, 10000)
用于进一步调试的附加代码:
const axios = require('axios')
// const dataFile = require('../data/car_data')
// dataFile.map(data => data.manufacturer_id)
const modelNameUrl = 'https://www.gov.il/api/mot/carlistprice/api/modelName?yazran_id='
let dataArray = []
const fetch = async (id) => {
const dataFetched = await axios.get(`${modelNameUrl}${id}`)
return dataFetched.data.dgamim_yazran
}
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
let integerSource = [
6, 67, 4, 5, 9, 60, 7, 30, 107, 113, 19,
120, 15, 17, 12, 59, 3, 129, 56, 1, 124, 29,
26, 64, 33, 63, 131, 112, 2, 39, 133, 38, 40,
48, 52, 53, 54, 50, 13, 110, 51, 57, 68, 23,
44, 22, 41, 21, 10, 32, 47, 45, 11
]
async function runLoop() {
for (let index of integerSource) {
try {
const data = await fetch(index);
console.log(data, index);
await delay(5000);
} catch (e) {
console.log(`Error on index ${index}`, e);
throw new Error
}
}
}
runLoop().then(() => {
console.log("all done");
}).catch(err => {
console.log("ended with error\n", err);
});
我建议只使用 for
循环和 await
然后 promise-returning 延迟。这将 space 从您的 API 呼叫结束时开始,而不是从开始时开始。您现有的方案不会立即增加索引,因此它甚至可以重复调用 fetch()
.
你说你在一个数组中有所需的整数堆栈,但没有在你的代码中显示它。您可以使用仅递增索引的 for
循环,也可以使用 for
循环从数组中提取整数。
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
let integerSource = [...]; // your array of integer values
async function runLoop() {
for (let index of integerSource) {
try {
const data = await fetch(index);
conole.log(data, index);
await delay(10000);
} catch(e) {
conole.log(`Error on index ${index}`, e);
// decide here whether you continue with further requests
// or throw e to stop further processing
}
}
}
// run the loop here
runLoop().then(() => {
console.log("all done");
}).catch(err => {
console.log("ended with error");
});