Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用承诺时如何避免嵌套函数过多_Node.js_Discord.js - Fatal编程技术网

Node.js 使用承诺时如何避免嵌套函数过多

Node.js 使用承诺时如何避免嵌套函数过多,node.js,discord.js,Node.js,Discord.js,所以我正在使用Discord.js开发一个Discord机器人。下面是我想简化的代码: message.channel.send('**Card 1:**(键入'next'以显示第二张卡)\n'+specs[Math.floor(Math.random()*specs.length)][pictureKey])。然后(()=>{ message.channel.waitingmessages(过滤器{ 最高:1, 时间:30000, 错误:['time'] }) 。然后(收集=>{ messag

所以我正在使用Discord.js开发一个Discord机器人。下面是我想简化的代码:

message.channel.send('**Card 1:**(键入'next'以显示第二张卡)\n'+specs[Math.floor(Math.random()*specs.length)][pictureKey])。然后(()=>{
message.channel.waitingmessages(过滤器{
最高:1,
时间:30000,
错误:['time']
})
。然后(收集=>{
message.channel.send('**Card 2:**(键入'next`以显示第三张卡)\n'+规格[Math.floor(Math.random()*specs.length)][pictureKey])。然后(()=>{
message.channel.waitingmessages(过滤器{
最高:1,
时间:30000,
错误:['time']
})
。然后(收集=>{
message.channel.send('**Card 3:**(键入'next`以显示第四张卡)\n'+规格[Math.floor(Math.random()*specs.length)][pictureKey])。然后(()=>{
message.channel.waitingmessages(过滤器{
最高:1,
时间:30000,
错误:['time']
})
。然后(收集=>{
message.channel.send('**Card 4:**(键入'next`以显示最后一张卡)\n'+规格[Math.floor(Math.random()*specs.length)][pictureKey])。然后(()=>{
message.channel.waitingmessages(过滤器{
最高:1,
时间:30000,
错误:['time']
})
。然后(收集=>{
message.channel.send('**卡5:*\n'+规格[Math.floor(Math.random()*规格长度)][pictureKey]);
})
})
})
})
})
})
})
.catch(已收集=>{
message.channel.send('Pack affected');
});
});
上面的代码可以工作,但太混乱了。有没有办法简化这一点

编辑:

异步函数revealCard(i){ message.channel.send(`**Card${i+1}:*(键入\'next\`以显示卡${i+2})\n`+specs[Math.floor(Math.random()*specs.length)][pictureKey]; let collected=await message.channel.await messages(过滤器{ 最高:1, 时间:30000, 错误:['time'] }); } 对于(i=0;i>5;i++){ 回收卡(一); 如果(已收集===null){ 打破 }
}正如其中一条评论中所建议的,您应该尝试使用:每当您有一个
时。然后()
如果您使用的是
async
函数,您可以用
wait
关键字替换它。
我会这样做:

//在异步函数中很重要(它将返回一个承诺)
异步函数yourStuff(){
//每次您想要等待承诺解决时,请使用wait关键字
等待消息.channel.send('**Card 1:**(键入'next'以显示第二张卡)\n'+规格[Math.floor(Math.random()*specs.length)][pictureKey])
等待消息。通道。等待消息(过滤器{
最高:1,
时间:30000,
错误:['time']
})
等待消息.channel.send('**Card 2:**(键入'next'以显示第三张卡)\n'+规格[Math.floor(Math.random()*specs.length)][pictureKey])
等待消息。通道。等待消息(过滤器{
最高:1,
时间:30000,
错误:['time']
})
//等等。。。
//如果希望得到承诺的结果,可以将其存储在如下变量中:
let collected=await message.channel.await message(…)
}

其他人提到了使用wait,但另一个解决方案是返回承诺

例如:

client.channels.fetch(“id”)
.then(channel=>channel.messages.fetch(“id”))
.then(message=>message.delete())
。然后(()=>console.log(“完成”);
就像Federico Grandi建议的那样,我认为你应该重新学习基础知识,而不仅仅是使用discord.js这样的库,
99%的错误都是因为你不懂js,就像这里的大多数帖子一样。

你可以使用async/await语法:首先,你把代码放在命令代码的地方了吗?此外,通过将循环移到异步函数之外,您最终会遇到相同的问题,添加一个事实,即在运行下一个函数时,您没有等待收集器完成,并且您会检查一个未在同一范围内声明的
收集的
变量。你应该把你想让程序等待的所有东西都放在异步函数中。不要误会,但在开始这类东西之前,你应该花一些时间来复习JS的基础知识和承诺:我告诉你,因为我认为你可以更轻松地复习这些主题,而不是必须“艰苦地学习”。这方面有很多很好的资源:MDN、FreeCodeCamp等等……嘿,我已经在答案部分发布了我的更新代码,你能帮我吗?