Created
November 13, 2018 12:03
-
-
Save jbardon/dedede64f070de31a26e9d88d3ae0562 to your computer and use it in GitHub Desktop.
#12 You know nothing (medium)
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 withCallback (message, callback) { | |
callback(message); | |
} | |
function promisify (functionToCall) { | |
return function promisifiedFunction () { | |
// Before new Promise otherwise arguments value change | |
var callArguments = Array.from(arguments); | |
return new Promise (function (resolve, reject) { | |
// Defined here for resolve to be defined | |
var callback = function (result) { | |
resolve(result); | |
}; | |
var argumentsForFunction = callArguments.concat(callback); | |
functionToCall.apply(null, argumentsForFunction); | |
}); | |
} | |
} | |
// Callback | |
withCallback('Callback', data => console.log(data)); | |
// Promise | |
var withPromise = promisify(withCallback); | |
withPromise('One promise') | |
.then(data => console.log(data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment