Last active
January 30, 2017 20:59
-
-
Save maggiben/64c7414f38cd20721b9631144ae38544 to your computer and use it in GitHub Desktop.
Chain promises
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 getUsers() { | |
return new Promise((resolve, reject) => { | |
setTimeout(function () { | |
return resolve('benja') | |
}, 1000) | |
}) | |
} | |
function getIssues(user) { | |
var issues = { | |
benja: [1, 2, 3, 4, 5, 6] | |
} | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
return resolve(issues[user]) | |
}, 1000) | |
}) | |
} | |
function getComments(issues) { | |
var comments = issues.map(issue => { | |
return `issue: ${issue} not solved`; | |
}) | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
return resolve(comments) | |
}, 1000) | |
}) | |
} | |
// Use | |
getUsers().then(getIssues).then(getComments).then(x => {console.log(x)}).catch(x => {console.log(x)}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment