From 270fe160dc2013420e93da25e0470c9695c1a7e1 Mon Sep 17 00:00:00 2001 From: elbywan Date: Sat, 8 Dec 2018 08:37:13 +0100 Subject: [PATCH] :memo: More details about the response types in the readme --- README.md | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ad8310f..52cfcfd 100644 --- a/README.md +++ b/README.md @@ -776,12 +776,23 @@ wretch("/resource") **Required** +All these methods accept an optional callback, and will return a Promise resolved with either the return value of the provided callback or the expected type. + +```js +// Without a callback +wretch("...").get().json().then(json => /* json is the parsed json of the response body */) +// Without a callback using await +const json = await wretch("...").get().json() +//With a callback the value returned is passsed to the Promise +wretch("...").get().json(() => "Hello world!").then(console.log) // Hello world! +``` + *If an error is caught by catchers, the response type handler will not be called.* | [res](#rescb-response--response--any) | [json](#jsoncb-json--object--any) | [blob](#blobcb-blob--blob--any) | [formData](#formdatacb-fd--formdata--any) | [arrayBuffer](#arraybuffercb-ab--arraybuffer--any) | [text](#textcb-text--string--any) | |-----|-----|-----|-----|-----|-----| -#### res(cb: (response : Response) => any) +#### res(cb?: (response : Response) => T) : Promise Raw Response handler. @@ -789,7 +800,7 @@ Raw Response handler. wretch("...").get().res(response => console.log(response.url)) ``` -#### json(cb: (json : Object) => any) +#### json(cb?: (json : Object) => T) : Promise Json handler. @@ -797,7 +808,7 @@ Json handler. wretch("...").get().json(json => console.log(Object.keys(json))) ``` -#### blob(cb: (blob : Blob) => any) +#### blob(cb?: (blob : Blob) => T) : Promise Blob handler. @@ -805,7 +816,7 @@ Blob handler. wretch("...").get().blob(blob => /* ... */) ``` -#### formData(cb: (fd : FormData) => any) +#### formData(cb: (fd : FormData) => T) : Promise FormData handler. @@ -813,7 +824,7 @@ FormData handler. wretch("...").get().formData(formData => /* ... */) ``` -#### arrayBuffer(cb: (ab : ArrayBuffer) => any) +#### arrayBuffer(cb: (ab : ArrayBuffer) => T) : Promise ArrayBuffer handler. @@ -821,7 +832,7 @@ ArrayBuffer handler. wretch("...").get().arrayBuffer(arrayBuffer => /* ... */) ``` -#### text(cb: (text : string) => any) +#### text(cb: (text : string) => T) : Promise Text handler.