Created
May 18, 2017 16:30
-
-
Save ultim8k/c5e0cca2e299b5f0c6367631b1e4f8ba to your computer and use it in GitHub Desktop.
Chaining sync with async functions
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 giveLater (num) { | |
return new Promise (function (resolve, reject) { | |
setTimeout(function () { | |
if (num) { | |
resolve(num); | |
return false; | |
} | |
reject('num is empty'); | |
}, 500); | |
}); | |
} | |
function numberToText (one) { | |
return new Promise (function (resolve, reject) { | |
if (!one) { | |
reject('none'); | |
return false; | |
} | |
resolve('one'); | |
}); | |
} | |
// Chaining sync with async functions | |
giveLater(2) | |
.then(num => num - 2) | |
.then(numberToText) | |
.then(txt => console.log(txt)) | |
.catch(err => console.error(err)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment