有没有办法跳到特定行?

时间:2019-11-25 11:56:20

标签: javascript typescript es2017

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


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

1 个答案:

答案 0 :(得分:2)

拥有一系列消息并遍历它们

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