Created
January 21, 2018 21:29
-
-
Save evgeniyp/41fa64acb6ab517a27a3f8cb35adf812 to your computer and use it in GitHub Desktop.
JS callback functions and promises chaining
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
"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