how to call async function in .on event nodejs
I am reading a CSV file using csv-parser npm module createReadStream.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', data =>
const val = await fun(param1, param2, param3);
);
fun(param1, param2, param3)
return true;
I have to call function fun with await but it is throwing me the error.await is only valid in async function. Can anyone help me how to fix this?
javascript node.js events
add a comment |
I am reading a CSV file using csv-parser npm module createReadStream.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', data =>
const val = await fun(param1, param2, param3);
);
fun(param1, param2, param3)
return true;
I have to call function fun with await but it is throwing me the error.await is only valid in async function. Can anyone help me how to fix this?
javascript node.js events
add a comment |
I am reading a CSV file using csv-parser npm module createReadStream.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', data =>
const val = await fun(param1, param2, param3);
);
fun(param1, param2, param3)
return true;
I have to call function fun with await but it is throwing me the error.await is only valid in async function. Can anyone help me how to fix this?
javascript node.js events
I am reading a CSV file using csv-parser npm module createReadStream.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', data =>
const val = await fun(param1, param2, param3);
);
fun(param1, param2, param3)
return true;
I have to call function fun with await but it is throwing me the error.await is only valid in async function. Can anyone help me how to fix this?
javascript node.js events
javascript node.js events
asked Nov 13 '18 at 13:05
m9m9mm9m9m
497512
497512
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Just indicate that the callback function is async
.
stream.on('data', async data =>
// Do async stuff
)
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
add a comment |
The function that you await must return a promise.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data =>
try
const val = await fun(param1, param2, param3);
catch(err)
//handle error
);
fun(param1, param2, param3)
return new Promise((resolve, reject)=>
resolve(some_value)
)
1
It needs to be specified asasync
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
– Aluan Haddad
Nov 13 '18 at 13:27
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%2f53281665%2fhow-to-call-async-function-in-on-event-nodejs%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Just indicate that the callback function is async
.
stream.on('data', async data =>
// Do async stuff
)
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
add a comment |
Just indicate that the callback function is async
.
stream.on('data', async data =>
// Do async stuff
)
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
add a comment |
Just indicate that the callback function is async
.
stream.on('data', async data =>
// Do async stuff
)
Just indicate that the callback function is async
.
stream.on('data', async data =>
// Do async stuff
)
answered Nov 13 '18 at 13:07
EnslevEnslev
40527
40527
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
add a comment |
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
each data has a JSON object and I have written a function call in call back function. Which is calling by all the objects at once. Can you tell me why? and I want to call the function in callback function one after once.
– m9m9m
Nov 14 '18 at 4:58
add a comment |
The function that you await must return a promise.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data =>
try
const val = await fun(param1, param2, param3);
catch(err)
//handle error
);
fun(param1, param2, param3)
return new Promise((resolve, reject)=>
resolve(some_value)
)
1
It needs to be specified asasync
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
– Aluan Haddad
Nov 13 '18 at 13:27
add a comment |
The function that you await must return a promise.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data =>
try
const val = await fun(param1, param2, param3);
catch(err)
//handle error
);
fun(param1, param2, param3)
return new Promise((resolve, reject)=>
resolve(some_value)
)
1
It needs to be specified asasync
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
– Aluan Haddad
Nov 13 '18 at 13:27
add a comment |
The function that you await must return a promise.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data =>
try
const val = await fun(param1, param2, param3);
catch(err)
//handle error
);
fun(param1, param2, param3)
return new Promise((resolve, reject)=>
resolve(some_value)
)
The function that you await must return a promise.
export default async function main()
const readstream = fs.createReadStream('src/working_file.csv');
stream.on('data', async data =>
try
const val = await fun(param1, param2, param3);
catch(err)
//handle error
);
fun(param1, param2, param3)
return new Promise((resolve, reject)=>
resolve(some_value)
)
edited Nov 13 '18 at 13:24
answered Nov 13 '18 at 13:19
squeekyDavesqueekyDave
417215
417215
1
It needs to be specified asasync
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
– Aluan Haddad
Nov 13 '18 at 13:27
add a comment |
1
It needs to be specified asasync
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited
– Aluan Haddad
Nov 13 '18 at 13:27
1
1
It needs to be specified as
async
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited– Aluan Haddad
Nov 13 '18 at 13:27
It needs to be specified as
async
and that very change means it does return a promise, by definition. An expression that is awaited is wrapped in a promise meaning that anything may be awaited– Aluan Haddad
Nov 13 '18 at 13:27
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%2f53281665%2fhow-to-call-async-function-in-on-event-nodejs%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