for循环的Java语言仅运行一次

时间:2020-08-30 14:29:15

标签: javascript

我正在从API提取数据,由于必须收集大量数据,因此我使用setTimeOut函数将每个请求延迟3秒。但是,当我尝试运行代码时,for循环仅运行一次并完成程序。

const fs = require('fs')
const { JSDOM } = require("jsdom")
const { window } = new JSDOM("")
const $ = require("jquery")(window)
const path = require('path')
const fetchArt = require('./utils/fetchArt.js')
const { data } = require('jquery')
const { resolve } = require('path')

let replacer = RegExp(' ', 'g')
let slash = RegExp('/', 'g')

const requestArtSongs = (artist) => {
    return $.getJSON("https://www.vagalume.com.br/" + artist + "/index.js").then(
        function (data) {
            return data.artist.lyrics.item
        }
    )
}

const map = (lang) => {
    switch (lang) {
        case 1: return 'pt'
        case 2: return 'en'
        default: return lang
    }
}

const requestMusic = (artist, song) => {
    return $.getJSON(
        "https://api.vagalume.com.br/search.php"
        + "?art=" + artist
        + "&mus=" + song
        + "&apikey={key}"
    ).then(function (data) {
        return [data.mus[0].text, data.mus[0].lang]
    })
}

const makeRequest = (art, songs) => {
    songs.forEach((song, i) => {
        delay(3000 * (i+1))
            .then(() => makeSongRequest(art, song.desc))
    })
}

const writeFile = (art, songName, mus) => {
    gen = gens[0]
    let music = {
        artist: art,
        song: {
            name: songName,
            text: mus[0],
            lang: map(mus[1])
        }
    }

    if (fs.existsSync(path.join(__dirname, 'Genders', gen, art))) {
        fs.writeFile(path.join(__dirname, 'Genders', gen, art, `${songName.replace(slash, '')}.json`),
            JSON.stringify(music, null, 4),
            (err) => {
                if (err) throw err
                console.log('Song ' + songName + ' written!')
            })
    } else {
        fs.mkdir(path.join(__dirname, 'Genders', gen, art), { recursive: true }, err => {
            if (err) throw err

            fs.writeFile(path.join(__dirname, 'Genders', gen, art, `${songName.replace(slash, '')}.json`),
                JSON.stringify(music, null, 4),
                (err) => {
                    if (err) throw err
                    console.log('Song ' + songName + ' written!')
                })
        })
    }

}

const makeSongRequest = (art, songName) => {
    requestMusic(art, songName)
        .then(mus => {
            writeFile(art, songName, mus)
        })
}

const delay = t => new Promise (resolve => setTimeout(resolve, t))

const writeSongs = (artist) => {
    art = artist.replace(replacer, '-').toLowerCase()
    return new Promise(() => {
        requestArtSongs(art)
            .then((songs) => {
                makeRequest(artist, songs)
            })
            
    })
}

const main = async () => {
    let gens = ['Funk']
    for (let gen of gens){
        let arts = await fetchArt(gen)
        for (let art of arts) {
            await writeSongs(art)
        }
        
    }
}

main()

在我的主要功能上,我试图从API中获取歌词。对于每种性别(在这种情况下,仅是“ Funk”),我都会获得演唱该性别的艺术家。然后,对于每个艺术家,我运行执行大量任务的writeSongs函数。 我正在使用Node JS来完成所有工作。

0 个答案:

没有答案