Skip to content

Instantly share code, notes, and snippets.

@jbardon
Created November 13, 2018 12:03
Show Gist options
  • Save jbardon/dedede64f070de31a26e9d88d3ae0562 to your computer and use it in GitHub Desktop.
Save jbardon/dedede64f070de31a26e9d88d3ae0562 to your computer and use it in GitHub Desktop.
#12 You know nothing (medium)
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