javascript - Accessibility of outside variable within a promise in nodejs -


can explain me why don´t work:

var outsidevar = 15; myfunc.dosomething().then(function() {   console.log("outsidevar: " + outsidevar); }).fail(function(err) {   console.log("error: ", err); }); 

the output error: undefined or outsidevar: undefined (depends if outsidevar variable or property of object.

an outside variable should accessible inside function or i´m wrong?

edit: i´m using construction spookyjs , seems there issue objects containing long strings.

in nodejs, use function error. so:

mypromise.then(function() {   // callback, executed on successful promise resolution }, function() {   // errback, executed on rejection }, function() {   // progressback, executed if promise has progress report }); 

.fail() isn't recognizable function in node, unless have defined .fail() function explicitly. .fail() throwing undefined error. .fail() in nodejs used building unit tests, not promises. https://nodejs.org/api/assert.html

otherwise, written properly, closure should work. typical closure syntax. see is promise closure? more details promises closures.

edit use .catch() instead of explicit error function.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -