如何跳过“第一条消息”或从第n行启动异步功能?


async function oneByOne() {
  await talk("First Message");
  await talk("Second Message?");
  await talk("Last Message?");
}

最佳答案

有一系列消息并遍历它们

async function oneByOne(startIndex) {
        const messages = ["first message", "second message?", "last message?"];
        while (startIndex < messages.length) {
            await talk(messages[startIndex]);
            startIndex++;
        }
}


现在,您可以输入oneByOne索引开始。如果您说oneByOne(1),它将跳过索引0

10-06 08:07