Why assigning a value to a global variable causes error vs no error if unassigned












0















I'm new to Node.js and I'm experimenting with it.



Since values from one promise are not available globally I'm trying a way to assign value to a variable that can be accessed anywhere down the chain.



What I found is the first block of code always ends in catch and second block works OK the only difference being assignment of value.



Can someone help me with this in doing it right way.



var test = (req,callBack)=>{
var value;
return querydb.checkstatus(req).then((result)=>{
value = 1;
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, value);
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};

var test = (req,callBack)=>{
var value;
querydb.checkstatus(req).then((result)=>{
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, "from then");
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};









share|improve this question

























  • what global variable?

    – epascarello
    Nov 14 '18 at 20:52











  • return(result); return is not a method

    – epascarello
    Nov 14 '18 at 20:53











  • may not be the right term but in this case I've defined a var value that will be used in promise chains.

    – David
    Nov 14 '18 at 20:54











  • Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

    – charlietfl
    Nov 14 '18 at 21:05













  • See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

    – Bergi
    Nov 14 '18 at 21:15
















0















I'm new to Node.js and I'm experimenting with it.



Since values from one promise are not available globally I'm trying a way to assign value to a variable that can be accessed anywhere down the chain.



What I found is the first block of code always ends in catch and second block works OK the only difference being assignment of value.



Can someone help me with this in doing it right way.



var test = (req,callBack)=>{
var value;
return querydb.checkstatus(req).then((result)=>{
value = 1;
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, value);
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};

var test = (req,callBack)=>{
var value;
querydb.checkstatus(req).then((result)=>{
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, "from then");
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};









share|improve this question

























  • what global variable?

    – epascarello
    Nov 14 '18 at 20:52











  • return(result); return is not a method

    – epascarello
    Nov 14 '18 at 20:53











  • may not be the right term but in this case I've defined a var value that will be used in promise chains.

    – David
    Nov 14 '18 at 20:54











  • Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

    – charlietfl
    Nov 14 '18 at 21:05













  • See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

    – Bergi
    Nov 14 '18 at 21:15














0












0








0








I'm new to Node.js and I'm experimenting with it.



Since values from one promise are not available globally I'm trying a way to assign value to a variable that can be accessed anywhere down the chain.



What I found is the first block of code always ends in catch and second block works OK the only difference being assignment of value.



Can someone help me with this in doing it right way.



var test = (req,callBack)=>{
var value;
return querydb.checkstatus(req).then((result)=>{
value = 1;
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, value);
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};

var test = (req,callBack)=>{
var value;
querydb.checkstatus(req).then((result)=>{
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, "from then");
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};









share|improve this question
















I'm new to Node.js and I'm experimenting with it.



Since values from one promise are not available globally I'm trying a way to assign value to a variable that can be accessed anywhere down the chain.



What I found is the first block of code always ends in catch and second block works OK the only difference being assignment of value.



Can someone help me with this in doing it right way.



var test = (req,callBack)=>{
var value;
return querydb.checkstatus(req).then((result)=>{
value = 1;
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, value);
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};

var test = (req,callBack)=>{
var value;
querydb.checkstatus(req).then((result)=>{
return(result);
}).then((result)=>{
if(result!=null){
callBack(null, "from then");
}
}).catch((errorMessage)=>{
callBack({Msg:"From Catch"},null);
})
};






javascript angularjs node.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:06







David

















asked Nov 14 '18 at 20:51









DavidDavid

106




106













  • what global variable?

    – epascarello
    Nov 14 '18 at 20:52











  • return(result); return is not a method

    – epascarello
    Nov 14 '18 at 20:53











  • may not be the right term but in this case I've defined a var value that will be used in promise chains.

    – David
    Nov 14 '18 at 20:54











  • Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

    – charlietfl
    Nov 14 '18 at 21:05













  • See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

    – Bergi
    Nov 14 '18 at 21:15



















  • what global variable?

    – epascarello
    Nov 14 '18 at 20:52











  • return(result); return is not a method

    – epascarello
    Nov 14 '18 at 20:53











  • may not be the right term but in this case I've defined a var value that will be used in promise chains.

    – David
    Nov 14 '18 at 20:54











  • Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

    – charlietfl
    Nov 14 '18 at 21:05













  • See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

    – Bergi
    Nov 14 '18 at 21:15

















what global variable?

– epascarello
Nov 14 '18 at 20:52





what global variable?

– epascarello
Nov 14 '18 at 20:52













return(result); return is not a method

– epascarello
Nov 14 '18 at 20:53





return(result); return is not a method

– epascarello
Nov 14 '18 at 20:53













may not be the right term but in this case I've defined a var value that will be used in promise chains.

– David
Nov 14 '18 at 20:54





may not be the right term but in this case I've defined a var value that will be used in promise chains.

– David
Nov 14 '18 at 20:54













Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

– charlietfl
Nov 14 '18 at 21:05







Seem to be going down the wrong path using promises. The function should return the checkstatus promise and not need a callback param. Then you can chain promises and errors will be passed down the chain and caught at the end of the chain

– charlietfl
Nov 14 '18 at 21:05















See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

– Bergi
Nov 14 '18 at 21:15





See How do I access previous promise results in a .then() chain? for how to solve your original problem correctly.

– Bergi
Nov 14 '18 at 21:15












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308528%2fwhy-assigning-a-value-to-a-global-variable-causes-error-vs-no-error-if-unassigne%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308528%2fwhy-assigning-a-value-to-a-global-variable-causes-error-vs-no-error-if-unassigne%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python