How to return object from API call using RxJS, Axios and TypeScript?

Mani

I'm making an API call to get the employee's details and using RxJS and TypeScript. I wanted to store them in a Map as key and value pairs like below:

function getEmps() {
  const empMap = new Map();

  rx
  .from(axios.get(api_url))
  .pipe(
   catchError((err) => {
          logger.error('Error in making api call', {
            error: err
          });

          throw new Error('Error in making api call');
        })
      )
   )
  .pipe(
    map((emps)=>{
      .....
      emps.forEach((empObj) =>{
         empMap.set(id, empObj);
      })
    })
  );
  return empMap;
}
console.log(getMap);

But finally, it's returning an empty map. What is the problem?

Some random IT boy

To transform a Promise<T> into an Observable<T> you need to use the defer and from utility functions from RxJS

import { defer, from } from 'rxjs';

function getSomething() {
  return defer(() => from(axios.get(...)))
}

Here's an example with using JavaScript's API's

import { defer, from } from 'rxjs';

async function getSomething(): Promise<Map> {
  const emps = await axios.get(...)
  const empMap = new Map();
  emps.forEach((empObj) => empMap.set(empObj.id, empObj))
  return empMap;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get object array from axios and using return value call another axios function and append the output to first result

Return response from Axios even if there is an exception when using multiple files for API call

How to call api parallelly in react using axios

Sequential API call using Rxjs in Angular with null return

How to call object function from Typescript object

How to mock an axios api call to return different values based on input?

Call an API from another API using Axios and React

how to make api call from external file using axios and callbacks and pass param in react

How to correctly type a null value in typescript react app, retrieved from an JSON object (api call) when using Object.entries to retrieve the value?

using rxjs and distinctUntilChanged to get items from api call

Using data from API JSON object using Axios and React

How to return an Image on this API Call using Laravel

How to Skip the API call using RxJS skip operator?

How to call multiple dependent api calls with time intervals using RxJS

How to return an object from a successful API call inside a `try` block React?

Swift - How to return json object from an API call in Model to use in View Controller

How to set a timeout using axios without cancelling API call?

How to make API call using axios in Vue.js?

How to return variable from Axios Response in Composition API to root level?

Fetch each object from API data using vue and axios

How to format data from axios request using typescript

How I get the javascript object I want from the javascript object I have and pass that to a nested (complex) body using Axios to post an API

How to return an object with its functions from API?

How to call axios/api call in Nativescript application

Return Promise Value from Page Object using TypeScript

Reduce from RxJS doesn't return Object

Using Axios return object inside script tag

Concat undetermined api call using rxjs

How can i get this Object from my api call am using retrofit