return data based on the fetch urls
up vote
0
down vote
favorite
I was trying to get the fetched data based on the changed events. The result I was expected should be an object instead I see observable as the fetch produces promise.
The flatMap
was used to convert promise
to an object, but it seems I was doing wrong. Please let me know how I can fix it.
/** Receives UpdateEvent */
const changes$ = new Subject<FileChangesEvent>();
/** mock for 'this' */
const context = ;
/** Process FilesChange event */
const stream$ = changes$
.pipe(
/** Side effect for log current step */
tap(event => console.log('onFileChanges trigged, event data: ', event.changes)),
/** Fetching array of changes */
map(event => event.changes),
/** Use it for wait all 'forked' observables */
flatMap(
/** Process each record in parallel */
changes => forkJoin(
changes.map(change =>
/** Update processing */
of(change)
.pipe(
tap(input => console.log('Validating the file Type: ', input)),
/** fetching data based on update type */
flatMap(input =>
input.type === FileChangeType.ADDED
? context.processAdds(input)
: context.processUpdates(input),
),
/** remove large files from update */
map(input => (
input.type === FileChangeType.UPDATED && input.size < CONTENT_SIZE_LIMIT
? Object.assign(input, value: '' )
: input
)),
/** side effect for pubsub notifications */
tap(updates => context.notify(updates)),
),
),
),
),
);
/** When updates stream was finished */
stream$.subscribe(
(...args) => console.log('[TICK]: ', ...args), // When all event updates was processed
(...args) => console.log('[ERROR]: ', ...args),
(...args) => console.log('[DONE]: ', ...args),
);
export const updateEventsStream = (event) => changes$.next(event);
observable rxjs6
add a comment |
up vote
0
down vote
favorite
I was trying to get the fetched data based on the changed events. The result I was expected should be an object instead I see observable as the fetch produces promise.
The flatMap
was used to convert promise
to an object, but it seems I was doing wrong. Please let me know how I can fix it.
/** Receives UpdateEvent */
const changes$ = new Subject<FileChangesEvent>();
/** mock for 'this' */
const context = ;
/** Process FilesChange event */
const stream$ = changes$
.pipe(
/** Side effect for log current step */
tap(event => console.log('onFileChanges trigged, event data: ', event.changes)),
/** Fetching array of changes */
map(event => event.changes),
/** Use it for wait all 'forked' observables */
flatMap(
/** Process each record in parallel */
changes => forkJoin(
changes.map(change =>
/** Update processing */
of(change)
.pipe(
tap(input => console.log('Validating the file Type: ', input)),
/** fetching data based on update type */
flatMap(input =>
input.type === FileChangeType.ADDED
? context.processAdds(input)
: context.processUpdates(input),
),
/** remove large files from update */
map(input => (
input.type === FileChangeType.UPDATED && input.size < CONTENT_SIZE_LIMIT
? Object.assign(input, value: '' )
: input
)),
/** side effect for pubsub notifications */
tap(updates => context.notify(updates)),
),
),
),
),
);
/** When updates stream was finished */
stream$.subscribe(
(...args) => console.log('[TICK]: ', ...args), // When all event updates was processed
(...args) => console.log('[ERROR]: ', ...args),
(...args) => console.log('[DONE]: ', ...args),
);
export const updateEventsStream = (event) => changes$.next(event);
observable rxjs6
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I was trying to get the fetched data based on the changed events. The result I was expected should be an object instead I see observable as the fetch produces promise.
The flatMap
was used to convert promise
to an object, but it seems I was doing wrong. Please let me know how I can fix it.
/** Receives UpdateEvent */
const changes$ = new Subject<FileChangesEvent>();
/** mock for 'this' */
const context = ;
/** Process FilesChange event */
const stream$ = changes$
.pipe(
/** Side effect for log current step */
tap(event => console.log('onFileChanges trigged, event data: ', event.changes)),
/** Fetching array of changes */
map(event => event.changes),
/** Use it for wait all 'forked' observables */
flatMap(
/** Process each record in parallel */
changes => forkJoin(
changes.map(change =>
/** Update processing */
of(change)
.pipe(
tap(input => console.log('Validating the file Type: ', input)),
/** fetching data based on update type */
flatMap(input =>
input.type === FileChangeType.ADDED
? context.processAdds(input)
: context.processUpdates(input),
),
/** remove large files from update */
map(input => (
input.type === FileChangeType.UPDATED && input.size < CONTENT_SIZE_LIMIT
? Object.assign(input, value: '' )
: input
)),
/** side effect for pubsub notifications */
tap(updates => context.notify(updates)),
),
),
),
),
);
/** When updates stream was finished */
stream$.subscribe(
(...args) => console.log('[TICK]: ', ...args), // When all event updates was processed
(...args) => console.log('[ERROR]: ', ...args),
(...args) => console.log('[DONE]: ', ...args),
);
export const updateEventsStream = (event) => changes$.next(event);
observable rxjs6
I was trying to get the fetched data based on the changed events. The result I was expected should be an object instead I see observable as the fetch produces promise.
The flatMap
was used to convert promise
to an object, but it seems I was doing wrong. Please let me know how I can fix it.
/** Receives UpdateEvent */
const changes$ = new Subject<FileChangesEvent>();
/** mock for 'this' */
const context = ;
/** Process FilesChange event */
const stream$ = changes$
.pipe(
/** Side effect for log current step */
tap(event => console.log('onFileChanges trigged, event data: ', event.changes)),
/** Fetching array of changes */
map(event => event.changes),
/** Use it for wait all 'forked' observables */
flatMap(
/** Process each record in parallel */
changes => forkJoin(
changes.map(change =>
/** Update processing */
of(change)
.pipe(
tap(input => console.log('Validating the file Type: ', input)),
/** fetching data based on update type */
flatMap(input =>
input.type === FileChangeType.ADDED
? context.processAdds(input)
: context.processUpdates(input),
),
/** remove large files from update */
map(input => (
input.type === FileChangeType.UPDATED && input.size < CONTENT_SIZE_LIMIT
? Object.assign(input, value: '' )
: input
)),
/** side effect for pubsub notifications */
tap(updates => context.notify(updates)),
),
),
),
),
);
/** When updates stream was finished */
stream$.subscribe(
(...args) => console.log('[TICK]: ', ...args), // When all event updates was processed
(...args) => console.log('[ERROR]: ', ...args),
(...args) => console.log('[DONE]: ', ...args),
);
export const updateEventsStream = (event) => changes$.next(event);
observable rxjs6
observable rxjs6
asked Nov 8 at 23:32
user1595858
99983573
99983573
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I see two problems in your code. First is destructuring a promise - map(i => Object.assign(i, ...fetch(i.url)))
. It destroys it completely and you will not be able to get a result out of it. If you need to modify the value returned from api use then
: map(i => fetch(i.url).then(val => Object.assign(i, ...val)))
.
Ok, next problem exactly with observables. You are correctly using flatMap
to unwrap value returned from processAdds
(and others), but the fact is these functions return promise wrapped in observable. You start with creating observable out of input: of(input)
and then map
input to promise. Hence when you unwrap result in flatMap
you get promise not it's value.
Simple solution would be to unwrap promise inside helper functions using any <something>Map
operators, e.g. the same flatMap
:
processAdds: (input) =>
of(input).pipe(delay(input.debounce || 1), flatMap(i => fetch(i.url))),
You can check full example here - https://stackblitz.com/edit/rxjs-fetch-flatmap?devtoolsheight=60
How doesflatMap
forward the error when the fetch fails with some error?
– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacingfetch
withPromise.reject
- stackblitz.com/edit/rxjs-iorier
– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I see two problems in your code. First is destructuring a promise - map(i => Object.assign(i, ...fetch(i.url)))
. It destroys it completely and you will not be able to get a result out of it. If you need to modify the value returned from api use then
: map(i => fetch(i.url).then(val => Object.assign(i, ...val)))
.
Ok, next problem exactly with observables. You are correctly using flatMap
to unwrap value returned from processAdds
(and others), but the fact is these functions return promise wrapped in observable. You start with creating observable out of input: of(input)
and then map
input to promise. Hence when you unwrap result in flatMap
you get promise not it's value.
Simple solution would be to unwrap promise inside helper functions using any <something>Map
operators, e.g. the same flatMap
:
processAdds: (input) =>
of(input).pipe(delay(input.debounce || 1), flatMap(i => fetch(i.url))),
You can check full example here - https://stackblitz.com/edit/rxjs-fetch-flatmap?devtoolsheight=60
How doesflatMap
forward the error when the fetch fails with some error?
– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacingfetch
withPromise.reject
- stackblitz.com/edit/rxjs-iorier
– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
add a comment |
up vote
0
down vote
I see two problems in your code. First is destructuring a promise - map(i => Object.assign(i, ...fetch(i.url)))
. It destroys it completely and you will not be able to get a result out of it. If you need to modify the value returned from api use then
: map(i => fetch(i.url).then(val => Object.assign(i, ...val)))
.
Ok, next problem exactly with observables. You are correctly using flatMap
to unwrap value returned from processAdds
(and others), but the fact is these functions return promise wrapped in observable. You start with creating observable out of input: of(input)
and then map
input to promise. Hence when you unwrap result in flatMap
you get promise not it's value.
Simple solution would be to unwrap promise inside helper functions using any <something>Map
operators, e.g. the same flatMap
:
processAdds: (input) =>
of(input).pipe(delay(input.debounce || 1), flatMap(i => fetch(i.url))),
You can check full example here - https://stackblitz.com/edit/rxjs-fetch-flatmap?devtoolsheight=60
How doesflatMap
forward the error when the fetch fails with some error?
– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacingfetch
withPromise.reject
- stackblitz.com/edit/rxjs-iorier
– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
add a comment |
up vote
0
down vote
up vote
0
down vote
I see two problems in your code. First is destructuring a promise - map(i => Object.assign(i, ...fetch(i.url)))
. It destroys it completely and you will not be able to get a result out of it. If you need to modify the value returned from api use then
: map(i => fetch(i.url).then(val => Object.assign(i, ...val)))
.
Ok, next problem exactly with observables. You are correctly using flatMap
to unwrap value returned from processAdds
(and others), but the fact is these functions return promise wrapped in observable. You start with creating observable out of input: of(input)
and then map
input to promise. Hence when you unwrap result in flatMap
you get promise not it's value.
Simple solution would be to unwrap promise inside helper functions using any <something>Map
operators, e.g. the same flatMap
:
processAdds: (input) =>
of(input).pipe(delay(input.debounce || 1), flatMap(i => fetch(i.url))),
You can check full example here - https://stackblitz.com/edit/rxjs-fetch-flatmap?devtoolsheight=60
I see two problems in your code. First is destructuring a promise - map(i => Object.assign(i, ...fetch(i.url)))
. It destroys it completely and you will not be able to get a result out of it. If you need to modify the value returned from api use then
: map(i => fetch(i.url).then(val => Object.assign(i, ...val)))
.
Ok, next problem exactly with observables. You are correctly using flatMap
to unwrap value returned from processAdds
(and others), but the fact is these functions return promise wrapped in observable. You start with creating observable out of input: of(input)
and then map
input to promise. Hence when you unwrap result in flatMap
you get promise not it's value.
Simple solution would be to unwrap promise inside helper functions using any <something>Map
operators, e.g. the same flatMap
:
processAdds: (input) =>
of(input).pipe(delay(input.debounce || 1), flatMap(i => fetch(i.url))),
You can check full example here - https://stackblitz.com/edit/rxjs-fetch-flatmap?devtoolsheight=60
edited Nov 12 at 4:04
answered Nov 10 at 5:22
Sergey Sokolov
1,716815
1,716815
How doesflatMap
forward the error when the fetch fails with some error?
– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacingfetch
withPromise.reject
- stackblitz.com/edit/rxjs-iorier
– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
add a comment |
How doesflatMap
forward the error when the fetch fails with some error?
– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacingfetch
withPromise.reject
- stackblitz.com/edit/rxjs-iorier
– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
How does
flatMap
forward the error when the fetch fails with some error?– user1595858
Nov 11 at 19:32
How does
flatMap
forward the error when the fetch fails with some error?– user1595858
Nov 11 at 19:32
It passes it down the stream. Example of replacing
fetch
with Promise.reject
- stackblitz.com/edit/rxjs-iorier– Sergey Sokolov
Nov 12 at 4:09
It passes it down the stream. Example of replacing
fetch
with Promise.reject
- stackblitz.com/edit/rxjs-iorier– Sergey Sokolov
Nov 12 at 4:09
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
I mean i don't want to fail the stack when we get the error. I rather want to continue by assign error to another observable and collect errors by manually by filtering with a type set before when it got failed.
– user1595858
Nov 13 at 5:20
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
Please ask another question with more details and code samples
– Sergey Sokolov
Nov 13 at 11:57
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
@user1595858 If I answer your original question please accept the answer
– Sergey Sokolov
Nov 15 at 14:59
add a comment |
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%2f53217726%2freturn-data-based-on-the-fetch-urls%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