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










share|improve this question
























  • Is there a question ?
    – trichetriche
    Nov 19 at 11:56















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










share|improve this question
























  • Is there a question ?
    – trichetriche
    Nov 19 at 11:56













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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
})),





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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
})),





share|improve this answer





















  • 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















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
})),





share|improve this answer





















  • 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













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
})),





share|improve this answer












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
})),






share|improve this answer












share|improve this answer



share|improve this answer










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 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy