妄想挽回

文章 评论 浏览 29

妄想挽回 2022-05-04 13:56:24
function test(str) {
  str = str.split('').map(item => {
    if (item === item.toUpperCase()) {
      return item.toLowerCase();
    } else {
      return item.toUpperCase();
    }
  }).join('');
  console.log(str);
}

第 69 题: 如何把一个字符串的大小写取反(大写变小写小写变大写),例如 ’AbC' 变成 'aBc'

妄想挽回 2022-05-04 13:54:13

比如重试 5 次

let time = () => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject("error");
    }, 1000);
  });
};

async function f(retry = 5) {
  while (retry !== 0) {
    try {
      const result = await time();
      return result;
    } catch (e) {
      if (--retry === 0) {
        throw e
      }
    }
  }
}

第 159 题:实现 Promise.retry,成功后 resolve 结果,失败后重试,尝试超过一定次数才真正的 reject

妄想挽回 2022-05-04 13:49:35
async function test() {
  const tt = await setTimeout(() => {console.log(333)}, 3000)
  console.log(444)
  console.log(tt)
}
test()
// 444
// 562
// Promise{<resolved>: undefined}
// 333

不是promise会转为promise,resolved后就继续往下执行了

async function test() {
  const tt = await Promise.resolve(setTimeout(() => {console.log(333)}, 3000))
  console.log(444)
  console.log(tt)
}
test()

ES6 系列之我们来聊聊 Async

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文