Skip to content

Instantly share code, notes, and snippets.

@evgeniyp
Created January 21, 2018 21:29
Show Gist options
  • Save evgeniyp/41fa64acb6ab517a27a3f8cb35adf812 to your computer and use it in GitHub Desktop.
Save evgeniyp/41fa64acb6ab517a27a3f8cb35adf812 to your computer and use it in GitHub Desktop.
JS callback functions and promises chaining
"use strict";
var typicalCallbackFunction = function(argument, callback) {
argument = '<' + argument + '>';
callback(null, argument);
}
var promisifiedTypicalCallbackFunction = (argument) => new Promise((resolve, reject) => {
typicalCallbackFunction(argument, (err, result) => err ? reject(err) : resolve(result));
});
var async1 = (arg) => new Promise((resolve, reject) => {
setTimeout(f => resolve('[' + arg + ' async1]'), 100);
});
var async2 = (arg) => new Promise((resolve, reject) => {
setTimeout(f => resolve('[' + arg + ' async2]'), 200);
});
promisifiedTypicalCallbackFunction('pcf')
.then(th => async1(th + '...'))
.then(async2)
.then(console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment