DEV Community

Mahabubur Rahman
Mahabubur Rahman

Posted on

Call Asynchronous With Typescript

Make Interface for What Api You Can Get

interface ITodo {
    userId: number;
    id: number;
    title: string;
    completed: boolean;
}
Enter fullscreen mode Exit fullscreen mode

Make Call With Async and Await Arrow Function

const getTodo = async (): Promise<ITodo> => {
    const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
    return await response.json();
};
Enter fullscreen mode Exit fullscreen mode

and Last Get The Data in Another Async Await Arrow Function

const getTodoData = async (): Promise<void> => {
    const result = await getTodo();
    console.log(result);
};

getTodoData();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)