Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#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
You can’t perform that action at this time.