How to combine operator input and result of defer operation together
up vote
1
down vote
favorite
I have tried to trim down my use case in below example.
I am stuck at the a point where I need to pass the result of operator which should have
- input passed of operator
- and result of the defer operation inside the
concatMap
.
so that I can use it next operator.
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 })
)
url: https://stackblitz.com/edit/rxjs-defer?file=index.ts
angular rxjs rxjs-pipeable-operators
add a comment |
up vote
1
down vote
favorite
I have tried to trim down my use case in below example.
I am stuck at the a point where I need to pass the result of operator which should have
- input passed of operator
- and result of the defer operation inside the
concatMap
.
so that I can use it next operator.
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 })
)
url: https://stackblitz.com/edit/rxjs-defer?file=index.ts
angular rxjs rxjs-pipeable-operators
Is there a question ?
– trichetriche
Nov 19 at 11:56
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have tried to trim down my use case in below example.
I am stuck at the a point where I need to pass the result of operator which should have
- input passed of operator
- and result of the defer operation inside the
concatMap
.
so that I can use it next operator.
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 })
)
url: https://stackblitz.com/edit/rxjs-defer?file=index.ts
angular rxjs rxjs-pipeable-operators
I have tried to trim down my use case in below example.
I am stuck at the a point where I need to pass the result of operator which should have
- input passed of operator
- and result of the defer operation inside the
concatMap
.
so that I can use it next operator.
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 })
)
url: https://stackblitz.com/edit/rxjs-defer?file=index.ts
angular rxjs rxjs-pipeable-operators
angular rxjs rxjs-pipeable-operators
edited Nov 20 at 5:21
asked Nov 19 at 11:49
Vivek Kumar
1,62932551
1,62932551
Is there a question ?
– trichetriche
Nov 19 at 11:56
add a comment |
Is there a question ?
– trichetriche
Nov 19 at 11:56
Is there a question ?
– trichetriche
Nov 19 at 11:56
Is there a question ?
– trichetriche
Nov 19 at 11:56
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
Or maybe if you want to use item
as a property:
map(result => ({
[item]: result
})),
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is anObservable
only.
– Vivek Kumar
Nov 20 at 5:25
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
Or maybe if you want to use item
as a property:
map(result => ({
[item]: result
})),
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is anObservable
only.
– Vivek Kumar
Nov 20 at 5:25
add a comment |
up vote
2
down vote
accepted
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
Or maybe if you want to use item
as a property:
map(result => ({
[item]: result
})),
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is anObservable
only.
– Vivek Kumar
Nov 20 at 5:25
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
Or maybe if you want to use item
as a property:
map(result => ({
[item]: result
})),
concatMap((item: number) => {
return defer(testFunctionPromise(item)).pipe(
map(result => ({
item: result
})),
);
})
Or maybe if you want to use item
as a property:
map(result => ({
[item]: result
})),
answered Nov 19 at 12:43
martin
40.7k1182124
40.7k1182124
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is anObservable
only.
– Vivek Kumar
Nov 20 at 5:25
add a comment |
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is anObservable
only.
– Vivek Kumar
Nov 20 at 5:25
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is an
Observable
only.– Vivek Kumar
Nov 20 at 5:25
Thanks it worked! I was so much confused with the real code that I forgot the defer in the end is an
Observable
only.– Vivek Kumar
Nov 20 at 5:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374029%2fhow-to-combine-operator-input-and-result-of-defer-operation-together%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is there a question ?
– trichetriche
Nov 19 at 11:56