for await of用法

function Gen(time) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            resolve(time)
        }, time)
    })
}

async function test() {
    let arr = [Gen(2000), Gen(100), Gen(3000)]
    for await (let item of arr) {
        console.log(Date.now(), item)
    }
}

test()
// 1560092345730 2000
// 1560092345730 100
// 1560092346336 3000
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你的代码可以做一些优化,这样可以提高代码的可读性和性能。这里提供一份优化后的代码: ``` async function getInProcessCount() { const list = await store.dispatch('ESourcingModule/GetMyRequest', searchView.value) return list.filter((o: { ViewType: string }) => o.ViewType === 'InClarification' || o.ViewType === 'SupplementEventDetails').length } function checkAdmin(routeritem: RouteRecordRaw) { document.title = 'RMS E-Sourcing' // 获取 InProcessCount 数据 let inProcessCount = 0 if (routeritem?.meta?.title === 'My Request') { getInProcessCount().then(count => inProcessCount = count) } // 检查用户权限 const userRoles = event.UsersInfo?.ROLES const routeRoles = routeritem?.meta?.roles || [] if (!userRoles) { return false } return userRoles.some(role => routeRoles.includes(role) && routeritem?.meta?.isSub !== 'true') } // 用法示例 const routerItem = { meta: { title: 'My Request', roles: ['admin'], isSub: false } } checkAdmin(routerItem).then(isAdmin => { if (isAdmin) { routerItem.meta.title = `${routerItem.meta.title} (${inProcessCount})` } }) ``` 这里的优化主要有以下几点: 1. 把获取 InProcessCount 数据的操作封装成了一个独立的函数 `getInProcessCount()`,方便代码复用和维护。 2. 使用 Promise 和 async/await 异步获取数据,避免了数据无法同步的问题。 3. 使用了可选链运算符 `?.` 和空值合并运算符 `||`,简化了代码判断逻辑,提高了代码的可读性。 4. 在检查用户权限时,使用了 `Array.prototype.some()` 方法和 `Array.prototype.includes()` 方法,简化了代码逻辑,提高了性能。 另外,建议你在代码中使用一致的命名风格,比如使用小驼峰式命名方法名和变量名,使用大驼峰式命名类型名和类名,这样可以提高代码的可读性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值