Clone and edit this document
Runkit
Runkit
home page
user forum
new notebook
clone notebook
download notebook
support & documentation
log in
sign up
new notebook
help & feedback
clone this notebook
download this notebook
Sign In
Sign Up
Promises and "this"
node v4.9.1
version:
master
endpoint
share
tweet
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