查看“︁Node.js/异步”︁的源代码
来自康健生活
←
Node.js/异步
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== await == 接收一个 promise 的时候,一种写法 <syntaxhighlight lang="javascript"> initConfig.map(async (v, i) => { const a = (await v).resources[0].el await (await layers[i]).appendChild(a) }); </syntaxhighlight> == async == == Promise == <syntaxhighlight lang="javascript"> // bad async function getUserInfo(id) { const profile = await getUserProfile(id); const repo = await getUserRepo(id) return { profile, repo } } // good async function getUserInfo(id) { const [profile, repo] = await Promise.all([ getUserProfile(id), getUserRepo(id) ]) return { profile, repo } }</syntaxhighlight> <syntaxhighlight lang="javascript"> async function getServiceIP(name) { // 从 DNS 和 ZooKeeper 获取服务 IP,哪个先成功返回用哪个 // 与 Promise.race 不同的是,这里只有当两个调用都 reject 时,才会抛出错误 return await Promise.any([ getIPFromDNS(name), getIPFromZooKeeper(name) ]) } </syntaxhighlight>
返回
Node.js/异步
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。