Promises and "this"

node v4.9.1
version: master
endpointsharetweet
This is a playground to test Promises and this references
"use strict"; class Flow { procedureA() { return new Promise((resolve, reject) => { return resolve("foo"); }); } procedureB(){ return this.procedureA(); } procedureC() { return new Promise((resolve, reject) => { return resolve("bar"); }); } procedureD() { return new Promise((resolve, reject) => { return resolve("zoo"); }); } } flow.procedureC().then(flow.procedureB) const flow = new Flow(); flow.procedureB().then(function(result){ console.log("SHOULD BE FOO", result) }); // executes without problems // .then changes "this" reference in flow.procedureB // Therefore the instance is not able to call procedureA flow.procedureC().then(flow.procedureB).then(function(){ console.log("SHOULD NOT HAPPEN") }).catch(function(e){ console.log("ERROR") console.log(e.stack) })
Loading…

no comments

    sign in to comment