-
-
Save dvallin/1e8353936f20ccee7978e6eb098734b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unwrapAxiosResponse<T>(request: AxiosPromise<object>, responseMapper: (object) => T): Promise<T> { | |
return new Promise<T>((resolve, reject) => { | |
request | |
.then(response => { | |
resolve(responseMapper(response.data)) | |
}) | |
.catch(error => { | |
reject(error) | |
}) | |
}); | |
} | |
export function fetchTasks(): Promise<Array<Task>> { | |
return unwrapAxiosResponse(axios.get('/api/tasks', config), (object) => { | |
return object.map(task => new Task(task.id, task.title)) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment