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









share|improve this question

























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









    share|improve this question























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









      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 23:32









      user1595858

      99983573




      99983573






















          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






          share|improve this answer






















          • 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










          • 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










          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%2f53217726%2freturn-data-based-on-the-fetch-urls%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
          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






          share|improve this answer






















          • 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










          • 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














          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






          share|improve this answer






















          • 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










          • 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












          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






          share|improve this answer














          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 4:04

























          answered Nov 10 at 5:22









          Sergey Sokolov

          1,716815




          1,716815











          • 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










          • 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










          • 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










          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Edmonton

          Crossroads (UK TV series)