I am busy working with some code that is responing in an unexpected way (to me). It involves handling Node.js promise exceptions.
I have the following function modified so that all it does is fail
function asynFunc() {
return new Promise(function(res, rej) {
rej("GENERIC ERROR");
});
}
The problem comes in when I am trying to call it and handle this error. If I do the following it works as I expect it to, the function fails and executes the catch which returns, stopping the last console log from executing
async function appStart() {
try {
await asyncFunc();
} catch(log) {
console.log(log);
return;
}
console.log("YOU SHOULD NOT SEE THIS");
}
appStart();
If I write the function as follows though, it does not seem to return, it still executes the last console log regardless of the await
async function appStart() {
await asyncFunc().catch(function(log) {
console.log(log);
return;
});
console.log("YOU SHOULD NOT SEE THIS");
}
If this is doing what I think it's doing then the return is returning from the function inside of the catch and not the appStart function itself, or I'm completely wrong in which case I have no idea why it's not returning.
Is there a way to use the second catch method and still have it return from the calling function?
In the second example, you are not returning from the outside function in the catch, you are returning from the catch callback:
await asyncFunc().catch(function(log) {
console.log(log);// ^
return; // | returns from that function
});
This has the effect of catching the error and moving on and returning a new promise resolving to undefined. There is no way to control the return of the outside function from inside the callback. You need to test the result of the async operation from the outside function, which leaves you with try/catch
or explicitly testing the result after the promise resolves.
I am trying to upload-file using angularFileUpload in php,but i will get this error in angular. [$injector:unpr] Unknown provider: $uploadProvider I have tried answer from this question but no ...
I am trying to upload-file using angularFileUpload in php,but i will get this error in angular. [$injector:unpr] Unknown provider: $uploadProvider I have tried answer from this question but no ...
function myFunc(){ console.log(myFunc.message); } myFunc.message = "Hi John"; myFunc(); Executing the above results in - Answer: 'Hi John' How is the function myFunc have the property message ...
function myFunc(){ console.log(myFunc.message); } myFunc.message = "Hi John"; myFunc(); Executing the above results in - Answer: 'Hi John' How is the function myFunc have the property message ...
Say I have a Class Library containing a HelloWorld.js file. This file contains a couple javascript functions, like say: function Hello_alert() { alert("Hello World!!"); } and function ...
Say I have a Class Library containing a HelloWorld.js file. This file contains a couple javascript functions, like say: function Hello_alert() { alert("Hello World!!"); } and function ...
I want to change the div order through JavaScript. i didn't know how to do that This is the two div which implement in html, but i want div id="navigation" show in html after div class="row ...
I want to change the div order through JavaScript. i didn't know how to do that This is the two div which implement in html, but i want div id="navigation" show in html after div class="row ...