在javascript中使用异步函数时,如何保持文件的有序?

在javascript中使用异步函数时,如何保持文件的有序?,javascript,discord,Javascript,Discord,因此,对于我使用的discord机器人,我可以创建一个自定义播放列表,并将其保存为txt文件。我想从文本文件中读取并将其中的每首歌曲排队。问题是这是一个异步函数,所以我失去了秩序,即使我希望它保持秩序,有什么办法可以做到这一点吗?这就是我的代码的样子,我不知道从哪里开始修改它,使它从第一首歌一直保持顺序。播放列表文件如下所示: 雷鸣 信徒 注定 乡间小路带我回家 近距离混音 对我很刻薄 代码如下所示。任何帮助都将不胜感激。谢谢 var lineReader = require('re

因此,对于我使用的discord机器人,我可以创建一个自定义播放列表,并将其保存为txt文件。我想从文本文件中读取并将其中的每首歌曲排队。问题是这是一个异步函数,所以我失去了秩序,即使我希望它保持秩序,有什么办法可以做到这一点吗?这就是我的代码的样子,我不知道从哪里开始修改它,使它从第一首歌一直保持顺序。播放列表文件如下所示:

雷鸣

信徒

注定

乡间小路带我回家

近距离混音

对我很刻薄

代码如下所示。任何帮助都将不胜感激。谢谢

      var lineReader = require('readline').createInterface({
        input: require('fs').createReadStream(actualPlaylist)
      });

      lineReader.on('line', async function(line){
        console.log('Line from file:', line);
        url = line ? line.replace(/<(.+)>/i, '$1') : '';
        console.log(url);
        try{
          var aVideo = await youtube.getVideo(url);
        } catch(error) {
          try{
            var myVideos = await youtube.searchVideos(line, 1);
            var aVideo = await youtube.getVideoByID(myVideos[0].id);
          } catch(myError) {
            console.error(myError);
            return msg.channel.send("Couldnt find any videos by that name.");
          }
        }
        return handleVideo(aVideo, msg, voiceChannel, false); // This is a async function we create down below next to the 'play' function!

      });
var lineReader=require('readline')。createInterface({
输入:require('fs')。createReadStream(实际播放列表)
});
lineReader.on('line',异步函数(line){
log('文件中的行:',行);
url=line?line.replace(//i,$1'):“”;
console.log(url);
试一试{
var aVideo=wait youtube.getVideo(url);
}捕获(错误){
试一试{
var myVideos=wait youtube.searchVideos(第1行);
var aVideo=wait youtube.getVideoByID(myVideos[0].id);
}捕获(myError){
控制台错误(myError);
返回msg.channel.send(“找不到任何同名视频”);
}
}
return handleVideo(aVideo,msg,voiceChannel,false);//这是我们在下面的“play”函数旁边创建的异步函数!
});

实现这一点的一种方法是创建一个队列,并在它启动下一个队列时取消移动歌曲。也许是这样的。注意:此代码未经测试。祝你好运

/* globals youtube msg handleVideo voiceChannel */
var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('./playlist.txt')
})

let songList = []
let called = false

async function getSong (item) {
  let aVideo
  try {
    aVideo = await youtube.getVideo(item.url)
  } catch (error) {
    try {
      const myVideos = await youtube.searchVideos(item.line, 1)
      aVideo = await youtube.getVideoByID(myVideos[0].id)
    } catch (myError) {
      console.error(myError)
      return msg.channel.send('Couldnt find any videos by that name.')
    }
  }
  return handleVideo(aVideo, msg, voiceChannel, false)
}

function videoQueue () {
  if (!songList.length) {
    called = false
    return
  }
  const item = songList.unshift()

  getSong(item)
    .then(() => {
      videoQueue()
    })
    .catch(() => {
      console.log('trying to play next song')
      videoQueue()
    })
}

lineReader.on('line', async function (line) {
  console.log('Line from file:', line)
  const url = line ? line.replace(/<(.+)>/i, '$1') : ''

  songList.push({url, line}) // {url, line} = {url:url, line:line}
  if (!called) videoQueue()
})
/*全球youtube msg handleVideo语音频道*/
var lineReader=require('readline')。createInterface({
输入:require('fs').createReadStream('./playlist.txt'))
})
让歌曲列表=[]
let=false
异步函数getSong(项){
让阿维迪奥
试一试{
aVideo=wait youtube.getVideo(item.url)
}捕获(错误){
试一试{
const myVideos=wait youtube.searchVideos(item.line,1)
aVideo=等待youtube.getVideoByID(myVideos[0].id)
}捕获(myError){
控制台错误(myError)
返回msg.channel.send('找不到任何同名视频')
}
}
返回handleVideo(aVideo、msg、voiceChannel、false)
}
函数videoQueue(){
if(!songList.length){
调用=错误
返回
}
const item=songList.unshift()
getSong(项目)
.然后(()=>{
视频队列()
})
.catch(()=>{
console.log('正在播放下一首歌')
视频队列()
})
}
lineReader.on('line',异步函数(line){
console.log('文件中的行:',行)
const url=line?line.replace(//i,$1'):“”
歌曲列表.push({url,line})/{url,line}={url:url,line:line}
如果(!调用)videoQueue()
})

实现这一点的一种方法是创建一个队列,并在它启动下一个队列时取消移动歌曲。也许是这样的。注意:此代码未经测试。祝你好运

/* globals youtube msg handleVideo voiceChannel */
var lineReader = require('readline').createInterface({
  input: require('fs').createReadStream('./playlist.txt')
})

let songList = []
let called = false

async function getSong (item) {
  let aVideo
  try {
    aVideo = await youtube.getVideo(item.url)
  } catch (error) {
    try {
      const myVideos = await youtube.searchVideos(item.line, 1)
      aVideo = await youtube.getVideoByID(myVideos[0].id)
    } catch (myError) {
      console.error(myError)
      return msg.channel.send('Couldnt find any videos by that name.')
    }
  }
  return handleVideo(aVideo, msg, voiceChannel, false)
}

function videoQueue () {
  if (!songList.length) {
    called = false
    return
  }
  const item = songList.unshift()

  getSong(item)
    .then(() => {
      videoQueue()
    })
    .catch(() => {
      console.log('trying to play next song')
      videoQueue()
    })
}

lineReader.on('line', async function (line) {
  console.log('Line from file:', line)
  const url = line ? line.replace(/<(.+)>/i, '$1') : ''

  songList.push({url, line}) // {url, line} = {url:url, line:line}
  if (!called) videoQueue()
})
/*全球youtube msg handleVideo语音频道*/
var lineReader=require('readline')。createInterface({
输入:require('fs').createReadStream('./playlist.txt'))
})
让歌曲列表=[]
let=false
异步函数getSong(项){
让阿维迪奥
试一试{
aVideo=wait youtube.getVideo(item.url)
}捕获(错误){
试一试{
const myVideos=wait youtube.searchVideos(item.line,1)
aVideo=等待youtube.getVideoByID(myVideos[0].id)
}捕获(myError){
控制台错误(myError)
返回msg.channel.send('找不到任何同名视频')
}
}
返回handleVideo(aVideo、msg、voiceChannel、false)
}
函数videoQueue(){
if(!songList.length){
调用=错误
返回
}
const item=songList.unshift()
getSong(项目)
.然后(()=>{
视频队列()
})
.catch(()=>{
console.log('正在播放下一首歌')
视频队列()
})
}
lineReader.on('line',异步函数(line){
console.log('文件中的行:',行)
const url=line?line.replace(//i,$1'):“”
歌曲列表.push({url,line})/{url,line}={url:url,line:line}
如果(!调用)videoQueue()
})

嘿,有点不清楚你想保持什么秩序。你的标题说你想保持一个文件的有序,但顺序是什么?从第一首歌到最后一首歌?也许给我们举一个你所指的文件的例子。很抱歉,我更新了一点问题,试图澄清它。嘿,有点不清楚你想保持什么秩序。你的标题说你想保持一个文件的有序,但顺序是什么?从第一首歌到最后一首歌?也许给我们举一个你提到的文件的例子。很抱歉,我更新了一点问题,试图澄清它。我会尽快尝试这个。谢谢如果答案对你有用,别忘了接受:)我会尽快尝试。谢谢如果答案对你有效,别忘了接受它:)