Skip to content

Instantly share code, notes, and snippets.

@tkissing
Created February 2, 2016 17: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 tkissing/b893fbf0c0f69f57b226 to your computer and use it in GitHub Desktop.
Save tkissing/b893fbf0c0f69f57b226 to your computer and use it in GitHub Desktop.
In response to https://t.co/KlxtCtCDJ4
// promise code
function add5 (x) {
return x + 5
}
function add5Promise (x) {
return Promise.resolve(x).then(function (x) {
return add5(x)
})
}
function someNumberLater () {
return new Promise(function (resolve) {
databaseConnector.get('numeric value').then(function (x) {
resolve(x)
})
})
}
add5Promise(someNumberLater()).then(function (xplus5) {
console.log('the number plus 5 is %d', xplus5)
})
function add5 (x) {
return x + 5
}
function someNumberLater () {
// if you are unsure what kind of thenable this returns and you want clear API,
// you could wrap in Promise.resolve()
return databaseConnector.get('numeric value');
}
someNumberLater().then(add5).then(function (xplus5) {
console.log('the number plus 5 is %d', xplus5)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment