Skip to content

Instantly share code, notes, and snippets.

@sigwyg
Last active January 7, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sigwyg/5c495cd940c02701588fc922ffbd30ae to your computer and use it in GitHub Desktop.
Save sigwyg/5c495cd940c02701588fc922ffbd30ae to your computer and use it in GitHub Desktop.
Fetch API cheatsheet
/**
* URLを叩いた結果で振り分ける。modal出すか、画面遷移するか。
* @param {String} url
*/
(async () => {
try {
const fetchUrl = encodeURIComponent(url);
const response = await fetch(fetchUrl);
const data = await response.json();
/**
* @param {Object} data The JSON ovject, ex.{ ab: "modal", landing_page_id: "20"}
*/
if(data.ab === 'modal') {
...
}
} catch (err) {
// fetch と response.json 両方のエラーをキャッチ
alert(err);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment