Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

fetch in ts

// Implementation code where T is the returned data shape
function api<T>(url: string): Promise<T> {
  return fetch(url)
    .then(response => {
      if (!response.ok) {
        throw new Error(response.statusText)
      }
      return response.json<T>()
    })

}

// Consumer
api<{ title: string; message: string }>('v1/posts/1')
  .then(({ title, message }) => {
    console.log(title, message)
  })
  .catch(error => {
    /* show error message */
  })
Comment

PREVIOUS NEXT
Code Example
Typescript :: class validator enum 
Typescript :: contents of file to variable python 
Typescript :: promise.all does not wait 
Typescript :: Socket.io bad request with response 
Typescript :: delete contents of folder java 
Typescript :: change textinputlayout color 
Typescript :: aggregate in r 
Typescript :: where are screenshots stored steam 
Typescript :: parsing error: unexpected token eslint typescript 
Typescript :: how are uv rays produced 
Typescript :: linux host file location 
Typescript :: how to remove one object in an array of objects in mongoose 
Typescript :: tostring kotlin 
Typescript :: create model class angular 
Typescript :: angular typescript refresh page 
Typescript :: typescript debounce 
Typescript :: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:3000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted. 
Typescript :: ionic web platform 
Typescript :: create plots with multiple dataframes python 
Typescript :: react-native-typescript 
Typescript :: how to get docker stats using shell script 
Typescript :: laravel validation check if email exists forget password 
Typescript :: c# copy the elements of a list to another list 
Typescript :: regex in typescript 
Typescript :: wordpress number of posts by user 
Typescript :: typescript loop over enum 
Typescript :: mongodb update all items in array 
Typescript :: create react app with redux and typescript 
Typescript :: isnull or empty typescript 
Typescript :: path para imports firebase firestore 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =