链接 RXJs 承诺 Observable.from
Chaining RXJs promises Observable.from
在 RxJS 6 中,如何使用 promises 链接和传递数据?我必须使用 jira-connector npm 库连续执行一堆 JIRA api 调用。但是我不确定如何执行并将数据传递给函数。
谢谢!
例如:
const pipeData = Observable.from(jira.search.search({
jql: 'team = 41 and type = "Initiative"'
}))
pipeData.pipe(
data => operators.flatMap(data.issues),
issue => Observable.from(jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`
}))).subscribe(results => {
console.log(results)
})
首先,你必须在管道函数中使用lettable操作符。
您试图做的似乎是:
- 拨打一个电话,获取一系列问题;
- 对每个问题单独打电话;
- 获取结果
所以像这样:
pipeData.pipe(
// when pipeData emits, subscribe to the following and cancel the previous subscription if there was one:
switchMap(data => of(data.issues))),
// now you get array of issues, so concatAll and get a stream of it:
concatAll(),
// now to call another HTTP call for every emit, use concatMap:
concatMap(issue => jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`
})).subscribe(results => {
console.log(results)
})
请注意,我没有用 from
包装 jira.search.search
因为您还可以将承诺传递给 concatMap
并且您还可以传递第二个参数 - resultSelector 功能 select 只是一些属性,如果需要的话:
concatMap(issue => jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`),
result => result.prop1
)
在 RxJS 6 中,如何使用 promises 链接和传递数据?我必须使用 jira-connector npm 库连续执行一堆 JIRA api 调用。但是我不确定如何执行并将数据传递给函数。
谢谢!
例如:
const pipeData = Observable.from(jira.search.search({
jql: 'team = 41 and type = "Initiative"'
}))
pipeData.pipe(
data => operators.flatMap(data.issues),
issue => Observable.from(jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`
}))).subscribe(results => {
console.log(results)
})
首先,你必须在管道函数中使用lettable操作符。
您试图做的似乎是:
- 拨打一个电话,获取一系列问题;
- 对每个问题单独打电话;
- 获取结果
所以像这样:
pipeData.pipe(
// when pipeData emits, subscribe to the following and cancel the previous subscription if there was one:
switchMap(data => of(data.issues))),
// now you get array of issues, so concatAll and get a stream of it:
concatAll(),
// now to call another HTTP call for every emit, use concatMap:
concatMap(issue => jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`
})).subscribe(results => {
console.log(results)
})
请注意,我没有用 from
包装 jira.search.search
因为您还可以将承诺传递给 concatMap
并且您还可以传递第二个参数 - resultSelector 功能 select 只是一些属性,如果需要的话:
concatMap(issue => jira.search.search({
jql: `team = 41 and "Parent Link" = ${issue.key}`),
result => result.prop1
)