Created
February 2, 2016 17:45
-
-
Save tkissing/b893fbf0c0f69f57b226 to your computer and use it in GitHub Desktop.
In response to https://t.co/KlxtCtCDJ4
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
// 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) | |
}) |
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
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