Async/await - Combination of awaiting functions and non awaiting fucntions [duplicate]
up vote
1
down vote
favorite
This question already has an answer here:
Can I fire and forget a promise in nodejs (ES7)?
2 answers
I want to combine asynchronous functions that i need to await and functions I don't need to await. First the code...
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!");
}
} catch(err) {
console.log(err)
}
}
Here is my questions. If my doAsynchronousStuffIDoNotNeedToAwait()
throws an error, does it trigger the catch function? In second function doSecondAsynchronousStuffIDoNotNeedToAwait()
is there a better way of handling the result? Does this code have some downsides or can it be written better way?
I hope that questions are clear and code is simple enough to understand. If not please leave comment and I will edit my question. Thank you :)
javascript asynchronous promise async-await
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
1
down vote
favorite
This question already has an answer here:
Can I fire and forget a promise in nodejs (ES7)?
2 answers
I want to combine asynchronous functions that i need to await and functions I don't need to await. First the code...
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!");
}
} catch(err) {
console.log(err)
}
}
Here is my questions. If my doAsynchronousStuffIDoNotNeedToAwait()
throws an error, does it trigger the catch function? In second function doSecondAsynchronousStuffIDoNotNeedToAwait()
is there a better way of handling the result? Does this code have some downsides or can it be written better way?
I hope that questions are clear and code is simple enough to understand. If not please leave comment and I will edit my question. Thank you :)
javascript asynchronous promise async-await
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
Can I fire and forget a promise in nodejs (ES7)?
2 answers
I want to combine asynchronous functions that i need to await and functions I don't need to await. First the code...
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!");
}
} catch(err) {
console.log(err)
}
}
Here is my questions. If my doAsynchronousStuffIDoNotNeedToAwait()
throws an error, does it trigger the catch function? In second function doSecondAsynchronousStuffIDoNotNeedToAwait()
is there a better way of handling the result? Does this code have some downsides or can it be written better way?
I hope that questions are clear and code is simple enough to understand. If not please leave comment and I will edit my question. Thank you :)
javascript asynchronous promise async-await
This question already has an answer here:
Can I fire and forget a promise in nodejs (ES7)?
2 answers
I want to combine asynchronous functions that i need to await and functions I don't need to await. First the code...
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!");
}
} catch(err) {
console.log(err)
}
}
Here is my questions. If my doAsynchronousStuffIDoNotNeedToAwait()
throws an error, does it trigger the catch function? In second function doSecondAsynchronousStuffIDoNotNeedToAwait()
is there a better way of handling the result? Does this code have some downsides or can it be written better way?
I hope that questions are clear and code is simple enough to understand. If not please leave comment and I will edit my question. Thank you :)
This question already has an answer here:
Can I fire and forget a promise in nodejs (ES7)?
2 answers
javascript asynchronous promise async-await
javascript asynchronous promise async-await
asked Nov 19 at 17:25
Raold
1561316
1561316
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Bergi
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 19 at 19:38
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23
add a comment |
1
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23
1
1
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Well, let's get it a try:
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
As you can see by running the snippet, no the error won't get caught by the try/catch, but you will get a global? error as the promise is not within the "scope" of the try catch, the only errors that get caught by the async await try/catch is when a normal exception is thrown or an awaited promised gets rejected. So you have to handle async errors the old fashion way, with a catch function.
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
add a comment |
up vote
1
down vote
It's easy enough to test:
await
ing the value works as expect :
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
Without await
you get an uncaught promise rejection error (which I think you can only see on SO if you look in the console):
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
This makes sense because without await
the function doesn't wait for the promise to resolve and the function finishes before there's a chance to catch the rejected promise.
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Well, let's get it a try:
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
As you can see by running the snippet, no the error won't get caught by the try/catch, but you will get a global? error as the promise is not within the "scope" of the try catch, the only errors that get caught by the async await try/catch is when a normal exception is thrown or an awaited promised gets rejected. So you have to handle async errors the old fashion way, with a catch function.
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
add a comment |
up vote
1
down vote
accepted
Well, let's get it a try:
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
As you can see by running the snippet, no the error won't get caught by the try/catch, but you will get a global? error as the promise is not within the "scope" of the try catch, the only errors that get caught by the async await try/catch is when a normal exception is thrown or an awaited promised gets rejected. So you have to handle async errors the old fashion way, with a catch function.
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Well, let's get it a try:
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
As you can see by running the snippet, no the error won't get caught by the try/catch, but you will get a global? error as the promise is not within the "scope" of the try catch, the only errors that get caught by the async await try/catch is when a normal exception is thrown or an awaited promised gets rejected. So you have to handle async errors the old fashion way, with a catch function.
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
Well, let's get it a try:
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
As you can see by running the snippet, no the error won't get caught by the try/catch, but you will get a global? error as the promise is not within the "scope" of the try catch, the only errors that get caught by the async await try/catch is when a normal exception is thrown or an awaited promised gets rejected. So you have to handle async errors the old fashion way, with a catch function.
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!"));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
function getDataFromDatabase() {
return new Promise((resolve) => {
setTimeout(resolve, 250, {canDoStuff: 'yes', canDoSecondStuff: 'yes'});
});
}
function doAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((resolve) => {
setTimeout(resolve, 250);
});
}
function doSecondAsynchronousStuffIDoNotNeedToAwait() {
return new Promise((_, reject) => {
setTimeout(reject, 250, 'error: doSecondAsynchronousStuffIDoNotNeedToAwait()');
});
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
if(data.canDoSecondStuff == "yes") {
doSecondAsynchronousStuffIDoNotNeedToAwait()
.then(result => console.log("Done!")).catch((err) => console.log(err));
}
} catch(err) {
console.log(err)
}
}
doSomeStuff();
answered Nov 19 at 17:33
dotconnor
1,037120
1,037120
add a comment |
add a comment |
up vote
1
down vote
It's easy enough to test:
await
ing the value works as expect :
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
Without await
you get an uncaught promise rejection error (which I think you can only see on SO if you look in the console):
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
This makes sense because without await
the function doesn't wait for the promise to resolve and the function finishes before there's a chance to catch the rejected promise.
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
add a comment |
up vote
1
down vote
It's easy enough to test:
await
ing the value works as expect :
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
Without await
you get an uncaught promise rejection error (which I think you can only see on SO if you look in the console):
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
This makes sense because without await
the function doesn't wait for the promise to resolve and the function finishes before there's a chance to catch the rejected promise.
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
add a comment |
up vote
1
down vote
up vote
1
down vote
It's easy enough to test:
await
ing the value works as expect :
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
Without await
you get an uncaught promise rejection error (which I think you can only see on SO if you look in the console):
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
This makes sense because without await
the function doesn't wait for the promise to resolve and the function finishes before there's a chance to catch the rejected promise.
It's easy enough to test:
await
ing the value works as expect :
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
Without await
you get an uncaught promise rejection error (which I think you can only see on SO if you look in the console):
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
This makes sense because without await
the function doesn't wait for the promise to resolve and the function finishes before there's a chance to catch the rejected promise.
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
let d = await doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error:", err)
}
}
doSomeStuff()
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
function getDataFromDatabase(){
return Promise.resolve({canDoStuff: "yes"})
}
function doAsynchronousStuffIDoNotNeedToAwait(){
return Promise.reject("whoops")
}
async function doSomeStuff() {
try {
const data = await getDataFromDatabase()
if(data.canDoStuff == "yes") {
doAsynchronousStuffIDoNotNeedToAwait()
}
} catch(err) {
console.log("caught error", err)
}
}
doSomeStuff()
answered Nov 19 at 17:34
Mark Meyer
32.3k32651
32.3k32651
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
add a comment |
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
Thank you, I think dotconnor's answer is more complex so it deserves to be marked as answer.
– Raold
Nov 19 at 17:54
1
1
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
No need to explain @Raold, glad you got it figured out.
– Mark Meyer
Nov 19 at 17:58
add a comment |
1
This is not a duplicate of the question suggested. Not all asynchronous executions are built on promises. Additionally, it is not necessarily obvious to someone coming to this question that "firing and forgetting" is the principle that is being attempted - identifying that would form a useful part of the answer to this question.
– J. Stott
Nov 20 at 9:23