In the below code have I createPost()
inside an async
function, but I don't want to use await
on it, so it blocks execution of the rest of the function. That I why I used then
.
Question
Since createPost()
is a async
/await
function (see below). How can it resolve/reject to then
/catch
in the outer function?
module.exports = async (p) => {
// lots of code here
try {
// lots of code here
createPost(p).then(message => {
// lots of code here
console.log("ok " message);
}).catch(message => {
console.log("failed " message);
});
} catch (error) {
console.log(error);
};
};
createPost()
module.exports = async (p) => {
// lots of code here
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
CodePudding user response:
async
/ await
is a more modern equivilant to .then
/ .catch
.
In this case you have mixed the syntaxes of both.
You don't need try
/ catch
blocks when using .then
,
just like you don't need to declare a function as async
when using the try
/ catch
syntax.
Other than that there's no reason they can't work together in seperate functions.
module.exports = (p) => {
createPost(p).then(message => {
console.log("ok " message);
}).catch(message => {
console.log("failed " message);
});
};
createPost():
module.exports = async (p) => {
try {
const r = await getStat();
} catch (error) {
console.log(error);
};
};
CodePudding user response:
An async
decleration on a function is redundant and wrong if you're not using the await
keyword inside.
await/async are often referred to as syntactic sugar, and let us wait for something (e.g. an API call), giving us the illusion that it is synchronous in an actual asynchronous code, which is a great benefit.
The things you want to acheive with async/await is is possible with promises but the advantages of async/await. let's an example with this code:
const makeRequest = () => //promise way
getJSON()
.then(data => {
return data
})
makeRequest();
const makeRequest = async () => { //async await way
const data = await getJSON();
return data;
}
makeRequest()
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Why is async/await prefered over promise?
Concise and clean - We didn’t have to write .then and create an anonymous function to handle the response, or give a name data to a variable that we don’t need to use. We also avoided nesting our code. async/await is a lot cleaner.
Error handling - Async/await makes it finally possible to handle both synchronous and asynchronous errors with the same try/catch format.
Debugging - A really good advantage when using async/await is that it’s much easier to debug then promises for 2 reasons: 1) you can’t set breakpoints in arrow functions that return expressions (no body). 2) if you set a breakpoint inside a .then block and use debug shortcuts like step-over, the debugger will not move to the the following .then because it only “steps” through synchronous code.
Error stacks - The error stack returned from promises chain gives us no idea of where the error occured and can be misleading. async/await gives us the error stack from async/await points to the function that contains the error which is a really big advantage.