JavaScript Node.js await catch not returning node await catch exception,node await catch,node await catch

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?

Answer:1

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.

Answer:2

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 ...

  1. angularjs file uploading
  2. angularjs file upload example
  3. angularjs file upload example jsfiddle
  4. angularjs file upload formdata
  5. angularjs file upload tutorial
  6. angularjs file upload with progress
  7. angularjs file upload validation
  8. angularjs file upload directive
  9. angularjs file upload example plunker
  10. angularjs file upload get path
  11. angularjs file upload drag and drop
  12. angularjs file upload get filename
  13. angularjs file upload progress bar
  14. angularjs file upload max size
  15. angularjs file upload with form data
  16. angularjs file upload progress bar example
  17. angularjs file upload w3schools
  18. angularjs file upload base64
  19. angularjs file upload ng-change
  20. angularjs file upload size validation

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 ...

  1. javascript functions have arguments
  2. do javascript functions have to return a value
  3. javascript can functions have properties
  4. functions have javascript

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 ...

  1. javascript using files
  2. using multiple javascript files
  3. using javascript files in html
  4. using gzip javascript files
  5. using javascript external files
  6. javascript and files
  7. angular use javascript files
  8. using javascript in pdf files

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 ...

  1. change array order javascript
  2. change div order javascript
  3. change object order javascript
  4. change element order javascript
  5. change list order javascript
  6. change tab order javascript
  7. change dom order javascript
  8. change li order javascript
  9. change event order javascript
  10. change column order javascript
  11. change image order javascript
  12. change node order javascript
  13. change z order javascript
  14. change sorting order javascript
  15. change order of keys javascript
  16. change table column order javascript
  17. change order of execution javascript
  18. change json key order javascript
  19. javascript change order of elements in array
  20. change order of html elements javascript