const self = this
Function decorators: Transforming callbacks into promises and back again
Joel Thoms
1623

Hi Joel, first of all thanks to sharing this!

Doesnt arrow functions use _lexical binding_ (capturing the _this_ around it)? so you can write the example without the _self_ reference…

function promisify(func) {
return (...args) =>
new Promise((resolve, reject) => {
const callback = ???
func.apply(this, [...args, callback]);
})
}

Anyway I´m missing the point of using _this_ here, why not to do this instead?

function promisify(func) {
return (...args) =>
new Promise((resolve, reject) => {
const callback = ???
func(...[...args, callback]);
})
}

If I´m not getting the thing wrong, when you do

const decorated = promisify(obj.fn);

the context of _promisify_ would be undefined on strict-mode / es2015 modules

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.