Sending DELETE Request In JS Using fetch

Sending DELETE request using then & catch

const id = 5;
const deleteTodo = (todoId) => {
const url = `https://jsonplaceholder.typicode.com/posts/${todoId}`;
const method = ‘DELETE’
fetch(url,{method})
.then(response => …


This content originally appeared on DEV Community and was authored by Aya Bouchiha

Sending DELETE request using then & catch

const id = 5;
const deleteTodo = (todoId) => {
  const url = `https://jsonplaceholder.typicode.com/posts/${todoId}`;
  const method  = 'DELETE'
  fetch(url,{method})
  .then(response => console.log(response.status)/*200*/)
  .catch(e=> console.log('something went wrong',e))
};
deleteTodo(id);

Sending DELETE request using async and await

const id = 5;
const deleteTodo = async (todoId) => {
  const url = `https://jsonplaceholder.typicode.com/posts/${todoId}`;
  const method  = 'DELETE';
  try {
    const response = fetch(url, {method});
    console.log((await response).status)// 200
  }catch(e){
    console.log('something went wrong', e);
  }
} 
deleteTodo(id);

Have a good day


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-08-20T23:33:04+00:00) Sending DELETE Request In JS Using fetch. Retrieved from https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/

MLA
" » Sending DELETE Request In JS Using fetch." Aya Bouchiha | Sciencx - Friday August 20, 2021, https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/
HARVARD
Aya Bouchiha | Sciencx Friday August 20, 2021 » Sending DELETE Request In JS Using fetch., viewed ,<https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/>
VANCOUVER
Aya Bouchiha | Sciencx - » Sending DELETE Request In JS Using fetch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/
CHICAGO
" » Sending DELETE Request In JS Using fetch." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/
IEEE
" » Sending DELETE Request In JS Using fetch." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/. [Accessed: ]
rf:citation
» Sending DELETE Request In JS Using fetch | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/08/20/sending-delete-request-in-js-using-fetch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.