Created
December 13, 2018 00:34
JS Async Promise
This file contains hidden or 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
findUser(1) | |
.then(function(user) { | |
console.log("user:", user); | |
}); | |
function findUser(id) { | |
return new Promise(function (resolve) { | |
setTimeout(function() { | |
console.log("waited 0.1 sec."); | |
const user = { | |
id: id, | |
name: "User" + id, | |
email: id + "@test.com" | |
}; | |
resolve(user); | |
}, 100); | |
}); | |
} |
This file contains hidden or 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 devide(numA, numB) { | |
return new Promise((resolve, reject) => { | |
if (numB === 0) | |
reject(new Error("Unable to devide by 0.")); | |
else | |
resolve(numA / numB); | |
}); | |
} | |
devide(8, 2) | |
.then(result => console.log("성공:", result)) | |
.catch(error => console.log("실패:", error)); | |
devide(8, 0) | |
.then(result => console.log("성공:", result)) | |
.catch(error => console.log("실패:", error)); |
This file contains hidden or 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
fetch('https://jsonplaceholder.typicode.com/posts/1') | |
.then(response => console.log("response:", response)) | |
.catch(error => console.log("error:", error)); |
This file contains hidden or 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
fetch() | |
.then(response => console.log("response:", response)) | |
.catch(error => console.log("error:", error)); |
This file contains hidden or 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
fetch('https://jsonplaceholder.typicode.com/posts/1') | |
.then(response => response.json()) | |
.then(post => console.log("post:", post)) | |
.catch(error => console.log("error:", error)); |
This file contains hidden or 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
fetch("https://jsonplaceholder.typicode.com/posts/1") | |
.then(response => response.json()) | |
.then(post => post.userId) | |
.then(userId => "https://jsonplaceholder.typicode.com/users/" + userId) | |
.then(url => fetch(url)) | |
.then(response => response.json()) | |
.then(user => console.log("user:", user)) | |
.catch(error => console.log("error:", error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment