DEV Community

Discussion on: Async function vs. a function that returns a Promise

Collapse
 
craigphicks profile image
Craig P Hicks

Just to report a detail, (in my environment) errors that occur within the body of Promise.resolve({<body>}) are not "Catched":

Promise.resolve((()=>{throw "oops"; })())
    .catch(e=>console("Catched ",e));
// escapes, "Error: oops" reported further up 

but error occurring in the body of a proper Promise are "Catched":

(new Promise((resolve,reject)=>{
    resolve((()=>{throw "oops"})())
}))
.catch(e=>console.log("Catched ",e));
// Catched  oops

How about this assertion:

async function fn() { <body> }

is semantically equivalent to

function fn() {
    return new Promise((resolve,reject)=>{
        resolve({ <body> })
    })
}

Corrollary:
The following is only a Proper Promise if <body> is a proper Promise:

function fn() {
    return Promise.resolve({<body});
}