
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
nested-asynchronous-requests
Advanced tools
更优雅的异步嵌套处理方式和错误捕获行为。
在这里我们称requestAuthor、requestPrice、requestPress为请求函数。且interface用于模拟某次请求的成功与否
// 标识每次请求的成功与否
const interface = [true, false, false]
const user = '张三'
const requestAuthor = () => new Promise((res, rej) => {
setTimeout(() => {
if (interface[0]) return res(`这个人叫${user}。`)
rej(`请求用户${user}`)
}, 1000)
})
const requestAge = () => new Promise((res, rej) => {
setTimeout(() => {
if (interface[1]) return res(`${user}的年龄`)
rej(`请求${user}》的年龄时出错`)
}, 1000)
})
const requestAddress = () => new Promise((res, rej) => {
setTimeout(() => {
if (interface[2]) return res(`${user}的住址在青岛`)
rej(`请求${book}的住址时出错`)
}, 1000)
})
requestAuthor()
.then(data => {
console.log('第一次请求', data)
return requestAge()
})
.then(data => {
console.log('第二次请求', data)
return requestAddress()
})
.then(data => {
console.log('第三次请求', data)
})
.catch(e => console.log('请求出错', e))
async的实现方式与上述代码大致相同,所以不再给出
使用链式then的方式,在我看来,有以下问题
then的返回值以及请求函数进行包装,或使用额外的环境进行存储Promise作一番处理then也继续增加综上所述,现在给出nested-asynchronous-requests的解决方案
npm install nested-asynchronous-requests
import nestedAsynchronousRequests from 'nested-asynchronous-requests'
const asyncQueue = [requestAuthor, requestAge, requestAddress]
const over = data => console.log(data)
nestedAsynchronousRequests(asyncQueue, { over })
接收两个参数
awaits:异步任务队列configs:配置项awaits必须是一个数组,该数组中的每位成员必须是函数或对象,举例
const asyncQueue = [ requestAuthor ]
const over = data => console.log(data)
nestedAsynchronousRequests(asyncQueue, { over })
当awaits是对象时,格式如下
const asyncQueue = [
requestAuthor,
{
func: requestPrice,
args: ['鲨鱼辣椒'],
callback: Function.prototype
}
]
const over = data => console.log(data)
nestedAsynchronousRequests(asyncQueue, { over })
func为要执行的请求函数
[args]表示当执行请求函数时要传递的参数;可选
[callback]会在请求函数执行完毕时调用;可选
传入nestedAsynchronousRequests的配置项,如不传入,则全部使用默认配置项
over: (data) => console.log(data)
必须指定该值为函数,所有请求完成后会执行该函数,此回调函数会收到最终的请求结果
遇到错误时,是否继续执行(发出请求)
forever: false
后一个请求函数是否接收前一个请求函数的结果
后一个请求函数是否接收前面所有请求函数的结果
当whole为true时,single无效,反之有效
pipes: {
single: false,
whole: true
}
需要注意的是,nested-asynchronous-requests不会抛出任何错误(除非你传递的参数不正确)
当所有请求函数都执行成功时,收到的结果如下
// 标识每次请求的失败与否
const interface = [true, true, true]
{
"result": [
{ "flag": true, "data": { "msg": "xxxxxxxxxxx" } },
{ "flag": true, "data": { "msg": "xxxxxxxxxxxxx" } },
{ "flag": true, "data": { "msg": "xxxxxxxxxxxxxxx" } }
],
"error": null
}
error为null,代表本次没有请求函数出现失败
result中存储着每次请求到的数据,flag标识当前请求是否成功,data.msg则为请求到的数据
// 标识每次请求的失败与否
const interface = [true, false, true]
{
"result": [
{ "flag": true, "data": { "msg": "xxxxxxxx } }
],
"error": {
"msg": "xxxxxxxxxx",
"funcName": "requestAge"
}
}
当某个请求失败时,error中将包含该请求函数的失败原因及名字
请求结果({result: ..., error: ...})的返回形式,及如何返回,会受到配置项的影响
FAQs
We found that nested-asynchronous-requests demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.