Skip to content

Instantly share code, notes, and snippets.

@ultim8k
Created May 18, 2017 16:30
Show Gist options
  • Save ultim8k/c5e0cca2e299b5f0c6367631b1e4f8ba to your computer and use it in GitHub Desktop.
Save ultim8k/c5e0cca2e299b5f0c6367631b1e4f8ba to your computer and use it in GitHub Desktop.
Chaining sync with async functions
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