The await keyword calls the Promise.then method in NodeJS <= v10 but this behavior is not observed in v12 or v14 or higher. I want a trap/hook for then .then method that gets invoked irrespective of await or explicit .then style callbacks. I was able to achieve this in Node <= 10 but does not work in Node 12 or 14 (randomly chosen). Am I doing something wrong?
➜ Desktop cat test.js
async function func() {
const aPromise = new Promise((resolve, reject) => {
setTimeout(() => { resolve(123); }, 100);
});
const clone = Object.create(aPromise);
const _then = Object.getPrototypeOf(clone).then;
_then.bind(aPromise);
aPromise.then = function () {
console.log('then called');
return _then.apply(this, arguments);
};
const result = await aPromise;
console.log('Log:', result);
}
func();%
➜ Desktop
➜ Desktop nvm use 10
Now using node v10.19.0 (npm v6.13.4)
➜ Desktop
➜ Desktop
➜ Desktop node test.js
then called
Log: 123
➜ Desktop
➜ Desktop
➜ Desktop nvm use 12
Now using node v12.18.4 (npm v6.14.6)
➜ Desktop
➜ Desktop
➜ Desktop node test.js
Log: 123
➜ Desktop
➜ Desktop
➜ Desktop nvm use 14
Now using node v14.12.0 (npm v6.14.8)
➜ Desktop
➜ Desktop
➜ Desktop node test.js
Log: 123
➜ Desktop