
获得徽章 0
- // sleep sort algorithm
const arr = [2, 5, 3, 8, 1, 6, 7, 4]
function sleepSort(arr) {
console.log('sleep sort start')
const result = []
Promise.all(
arr.map((val, idx) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(`num: ${val}, index in arr: ${idx}`)
result.push(val)
resolve(val)
}, val * 100)
})
})
).then((list) => {
console.log('sleep sort complete, result:', result)
})
}
sleepSort(arr)展开评论点赞 - # 好玩的代码评论
开发项目看到的:
// 取反判断流程, 这逻辑要日天吗?
// 多写几行代码, 更清晰不容易影响其它流程
// 连锁bug就是这样写出来的, 改一个流程其它流程全出错
虽然不知道说的是什么情形,但感觉莫名的有道理。展开赞过评论3 - let wangwang = {
location: "HangZhou China",
birthYear: 93,
six: "male",
job: "frontend engineer",
height: "172cm",
weight: `${getRandomRange(55, 58)}kg`,
hobbyList: `${generateSingleHobby('game', 'download')}\n${generateSingleHobby('book', 'buy', 'read')}\n${generateSingleHobby('snacks', 'buy', 'eat')}`,
neteaseMusicHomePage: "music.163.com"
}
function getRandomRange(min, max) {
const distance = max - min
return min + +(Math.random() * distance).toFixed(1)
}
function generateSingleHobby(hobby, action = 'like', inaction = 'play') {
return `I ${action} ${hobby}, but not very ${inaction} ${hobby}!`
}
if (you.isInterested(wangwang)) {
@please
contactMe()
}
module.exports = wangwang展开等人赞过84