Skip to content

Instantly share code, notes, and snippets.

@matrixcloud
Created January 10, 2023 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matrixcloud/e0588fe25f7e7c94d13846ecb7565460 to your computer and use it in GitHub Desktop.
Save matrixcloud/e0588fe25f7e7c94d13846ecb7565460 to your computer and use it in GitHub Desktop.
javascript to get pageable data by generator
import axios from "axios"
const BASE_API = 'http://localhost:9090/actuator'
async function scheduleJob(jobId) {
try {
await axios.put(`${BASE_API}/jobs/${jobId}/schedule`)
} catch(e) {
console.error(`failed to schedule job ${jobId} due to ${e.message}`)
}
}
async function* fetchJobs() {
try {
const res = await axios.get(`${BASE_API}/jobs`)
const { content, totalPages } = res.data
yield content
for (let i = 1; i < totalPages; i++) {
const res = await axios.get(`${BASE_API}/jobs?page=${i}`)
yield res.data.content
}
} catch (e) {
console.error(`failed to fetch jobs due to ${e.message}`)
}
}
(async function main() {
const jobs = []
for await (const val of fetchJobs()) {
jobs.push([...val])
}
jobs
.filter(job => job.status !== 'CREATED')
.forEach(async (job) => {
console.log(`start to schedule job: ${job.name}`)
await scheduleJob(job.id)
console.log(`finish to schedule job: ${job.name}`)
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment