@heymp/safe-fetch
v0.0.3
Published
Return errors as values for fetch calls.
Downloads
2
Readme
@heymp/safe-fetch
A minimal fetch wrapper that mimics functionality of the new ECMAScript Safe Assignment Operator Proposal.
Return errors as values from your fetch calls and text()
and json()
methods on the fetch Response
object.
Example
import { safeFetch } from '@heymp/safe-fetch';
export async function getTodos() {
const [requestError, response] = await safeFetch('https://jsonplaceholder.typicode.com/todos');
if (requestError) {
// handle request error
}
const [parseError, data] = await response.json();
if (parseError) {
// handle parse error
}
return data;
}