在下面的示例中,我试图缩小用例。
我被困在需要传递运算符结果的位置
concatMap
。以便我可以在下一个运算符中使用它。
import { of, from, defer } from 'rxjs';
import { mergeMap, filter, concatMap, map, reduce } from 'rxjs/operators';
const list: number[] = [1, 2, 3, 4, 5, 6];
function testFunctionPromise(value: number) {
return () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
return resolve('processed-value ' + value);
}, 1000);
});
}
}
from(list)
.pipe(
filter((item: number) => item % 2 === 0),
concatMap((item: number) => {
const pf = testFunctionPromise(item);
/**
* QUESTION: from this step I want to pass both
* the result of defer(pf); and item
*
* as
*
* return { item: <result of defer(pf)> }
*
*/
return defer(pf);
}),
reduce((acc: any[], item: any) => acc.concat([item]), [])
)
.subscribe(
data => console.log({ data }),
error => console.error({ error })
)
答案 0 :(得分:2)
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
或者如果您想使用item
作为属性:
map(result => ({
[item]: result
})),