Last active
January 7, 2019 12:45
-
-
Save sigwyg/5c495cd940c02701588fc922ffbd30ae to your computer and use it in GitHub Desktop.
Fetch API cheatsheet
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
/** | |
* 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