Skip to content

Instantly share code, notes, and snippets.

@neolee
Created June 26, 2020 15:37
Show Gist options
  • Save neolee/1c901f886fbd9ebac6a300e7aff083c8 to your computer and use it in GitHub Desktop.
Save neolee/1c901f886fbd9ebac6a300e7aff083c8 to your computer and use it in GitHub Desktop.
require('console-stamp')(console, '[HH:MM:ss.l]')
const request = require('request')
const categories = [4754, 4755, 4756, 4635, 4634, 4620, 4892, 4621, 4622]
function getData(id) {
console.log("Started fetching courses in category " + id)
let url = `https://www.genshuixue.com/sapi/channel/list?topChannelId=${id}&secondChannelId=${id}&thirdChannelId=all`
return new Promise((resolve, reject) => {
request(url, (error, response, body) => {
if (error) reject(error)
if (response.statusCode != 200) {
reject(`Response error, status code=<${response.statusCode}>`)
}
resolve(JSON.parse(body))
})
})
}
categories.forEach(function (value, index) {
getData(value).then((result) => {
total = result.data.pager.total
console.log("Completed id=" + value + " and fetched " + total + " courses")
})
})
@neolee
Copy link
Author

neolee commented Jun 26, 2020

Running result:

❯ node promise-concurrency.js
[[23:35:55.018]] [LOG]    Started fetching courses in category 4754
[[23:35:55.023]] [LOG]    Started fetching courses in category 4755
[[23:35:55.023]] [LOG]    Started fetching courses in category 4756
[[23:35:55.024]] [LOG]    Started fetching courses in category 4635
[[23:35:55.024]] [LOG]    Started fetching courses in category 4634
[[23:35:55.024]] [LOG]    Started fetching courses in category 4620
[[23:35:55.024]] [LOG]    Started fetching courses in category 4892
[[23:35:55.024]] [LOG]    Started fetching courses in category 4621
[[23:35:55.025]] [LOG]    Started fetching courses in category 4622
[[23:35:55.285]] [LOG]    Completed id=4620 and fetched 8 courses
[[23:35:55.295]] [LOG]    Completed id=4635 and fetched 9 courses
[[23:35:55.360]] [LOG]    Completed id=4634 and fetched 2 courses
[[23:35:55.371]] [LOG]    Completed id=4621 and fetched 9 courses
[[23:35:55.447]] [LOG]    Completed id=4755 and fetched 29 courses
[[23:35:55.451]] [LOG]    Completed id=4754 and fetched 3 courses
[[23:35:55.462]] [LOG]    Completed id=4756 and fetched 27 courses
[[23:35:55.465]] [LOG]    Completed id=4892 and fetched 19 courses
[[23:35:55.671]] [LOG]    Completed id=4622 and fetched 23 courses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment