【问题标题】:Is there a way to jump to specific line?有没有办法跳到特定的行?
【发布时间】:2020-03-20 17:07:23
【问题描述】:

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


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

【问题讨论】:

  • 要有条件地跳过行,请将它们包装在 if 块中。

标签: javascript typescript ecmascript-2017


【解决方案1】:

有一个消息数组并循环遍历它们

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

【讨论】:

    猜你喜欢
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    相关资源
    最近更新 更多