Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x | import fetch from 'node-fetch';
// #NOTES: 在 Node.js 中沒有內建的 fetch 函式
/**
* 任務:實作一個 async 函式 `fetchData`,該函式應該能夠從指定的 URL 取得資料。
* 範例:fetchData('https://jsonplaceholder.typicode.com/todos/1') 應該回傳一個包含 id、title 等屬性的物件
* @param url - 要取得資料的 URL
* @returns - 回傳一個 Promise,該 Promise resolve 的值應該是從 URL 取得的資料
*/
// 請在下方寫下你的程式碼
export async function fetchData(url: string): Promise<any> {
const response = await fetch(url);
if (!response.ok) {
// throw new Error(`HTTP error! status: ${response.status}`);
console.log(`HTTP error! status: ${response.status}`);
}
return await response.json();
}
fetchData('https://jsonplaceholder.typicode.com/todos/1').then(data => console.log(data)); |