WHy am I getting a SyntaxError: Unexpected identifier in forEach with await?
I have an object with a series of named SQL query templates that I'm trying to execute.
The query() method is async, so I should be able to run it with await, but I get an Unexpected identifier error. If I take off the await, then I don't get an error, but I'm stuck with a Promise object...
Object.entries(sqlquerries).forEach(([queryName, queryTpl]) => {
try {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
} catch (err) {
console.error('An error occured running the query : ', err );
}
});
return report;
Why would that be? What can I do to construct my report object and get around this error?
Any help/pointers would be greatly appreciated.
javascript promise async-await
add a comment |
I have an object with a series of named SQL query templates that I'm trying to execute.
The query() method is async, so I should be able to run it with await, but I get an Unexpected identifier error. If I take off the await, then I don't get an error, but I'm stuck with a Promise object...
Object.entries(sqlquerries).forEach(([queryName, queryTpl]) => {
try {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
} catch (err) {
console.error('An error occured running the query : ', err );
}
});
return report;
Why would that be? What can I do to construct my report object and get around this error?
Any help/pointers would be greatly appreciated.
javascript promise async-await
3
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07
add a comment |
I have an object with a series of named SQL query templates that I'm trying to execute.
The query() method is async, so I should be able to run it with await, but I get an Unexpected identifier error. If I take off the await, then I don't get an error, but I'm stuck with a Promise object...
Object.entries(sqlquerries).forEach(([queryName, queryTpl]) => {
try {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
} catch (err) {
console.error('An error occured running the query : ', err );
}
});
return report;
Why would that be? What can I do to construct my report object and get around this error?
Any help/pointers would be greatly appreciated.
javascript promise async-await
I have an object with a series of named SQL query templates that I'm trying to execute.
The query() method is async, so I should be able to run it with await, but I get an Unexpected identifier error. If I take off the await, then I don't get an error, but I'm stuck with a Promise object...
Object.entries(sqlquerries).forEach(([queryName, queryTpl]) => {
try {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
} catch (err) {
console.error('An error occured running the query : ', err );
}
});
return report;
Why would that be? What can I do to construct my report object and get around this error?
Any help/pointers would be greatly appreciated.
javascript promise async-await
javascript promise async-await
asked Nov 22 '18 at 9:06
brainstormtrooperbrainstormtrooper
100211
100211
3
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07
add a comment |
3
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07
3
3
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07
add a comment |
1 Answer
1
active
oldest
votes
Because the function await
appears in (the forEach
callback) isn't an async
function. If you make it one, beware that forEach
does nothing with the callback's return value, which will be a promise if it's an async
function, and so you need to be sure to handle errors inline (which you're doing).
But, your return report
suggests you're expecting to wait for this process to complete. Beware that forEach
will not wait for the previous iteration's promise to resolve before proceeding with the next. If you want to do that, use the promise reduce
trick instead and await
the result (I assume this is all in an async
function):
await Object.entries(sqlqueries).reduce((p, [queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
return p.then(async() => {
report[queryName] = await query(querystr);
});
}, Promise.resolve());
return report;
Or if you can run the queries in parallel, use map
and Promise.all
instead, and again await
the result:
await Promise.all(Object.entries(sqlqueries).map(async ([queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
}));
return report;
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427265%2fwhy-am-i-getting-a-syntaxerror-unexpected-identifier-in-foreach-with-await%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Because the function await
appears in (the forEach
callback) isn't an async
function. If you make it one, beware that forEach
does nothing with the callback's return value, which will be a promise if it's an async
function, and so you need to be sure to handle errors inline (which you're doing).
But, your return report
suggests you're expecting to wait for this process to complete. Beware that forEach
will not wait for the previous iteration's promise to resolve before proceeding with the next. If you want to do that, use the promise reduce
trick instead and await
the result (I assume this is all in an async
function):
await Object.entries(sqlqueries).reduce((p, [queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
return p.then(async() => {
report[queryName] = await query(querystr);
});
}, Promise.resolve());
return report;
Or if you can run the queries in parallel, use map
and Promise.all
instead, and again await
the result:
await Promise.all(Object.entries(sqlqueries).map(async ([queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
}));
return report;
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
add a comment |
Because the function await
appears in (the forEach
callback) isn't an async
function. If you make it one, beware that forEach
does nothing with the callback's return value, which will be a promise if it's an async
function, and so you need to be sure to handle errors inline (which you're doing).
But, your return report
suggests you're expecting to wait for this process to complete. Beware that forEach
will not wait for the previous iteration's promise to resolve before proceeding with the next. If you want to do that, use the promise reduce
trick instead and await
the result (I assume this is all in an async
function):
await Object.entries(sqlqueries).reduce((p, [queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
return p.then(async() => {
report[queryName] = await query(querystr);
});
}, Promise.resolve());
return report;
Or if you can run the queries in parallel, use map
and Promise.all
instead, and again await
the result:
await Promise.all(Object.entries(sqlqueries).map(async ([queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
}));
return report;
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
add a comment |
Because the function await
appears in (the forEach
callback) isn't an async
function. If you make it one, beware that forEach
does nothing with the callback's return value, which will be a promise if it's an async
function, and so you need to be sure to handle errors inline (which you're doing).
But, your return report
suggests you're expecting to wait for this process to complete. Beware that forEach
will not wait for the previous iteration's promise to resolve before proceeding with the next. If you want to do that, use the promise reduce
trick instead and await
the result (I assume this is all in an async
function):
await Object.entries(sqlqueries).reduce((p, [queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
return p.then(async() => {
report[queryName] = await query(querystr);
});
}, Promise.resolve());
return report;
Or if you can run the queries in parallel, use map
and Promise.all
instead, and again await
the result:
await Promise.all(Object.entries(sqlqueries).map(async ([queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
}));
return report;
Because the function await
appears in (the forEach
callback) isn't an async
function. If you make it one, beware that forEach
does nothing with the callback's return value, which will be a promise if it's an async
function, and so you need to be sure to handle errors inline (which you're doing).
But, your return report
suggests you're expecting to wait for this process to complete. Beware that forEach
will not wait for the previous iteration's promise to resolve before proceeding with the next. If you want to do that, use the promise reduce
trick instead and await
the result (I assume this is all in an async
function):
await Object.entries(sqlqueries).reduce((p, [queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
return p.then(async() => {
report[queryName] = await query(querystr);
});
}, Promise.resolve());
return report;
Or if you can run the queries in parallel, use map
and Promise.all
instead, and again await
the result:
await Promise.all(Object.entries(sqlqueries).map(async ([queryName, queryTpl]) => {
const querystr = replVars(queryTpl, vars);
report[queryName] = await query(querystr);
}));
return report;
answered Nov 22 '18 at 9:10
T.J. CrowderT.J. Crowder
690k12112251319
690k12112251319
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
add a comment |
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
1
1
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
Excellent!!! Not only is the answer just what I needed, but the analysis of the situation saved me my next question :-D Thanks loads!!!
– brainstormtrooper
Nov 22 '18 at 9:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427265%2fwhy-am-i-getting-a-syntaxerror-unexpected-identifier-in-foreach-with-await%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
forEach(async ([
– Lawrence Cherone
Nov 22 '18 at 9:07