Convert promise function to async/awaitIs there an “exists” function for jQuery?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert JS object to JSON stringHow and when to use ‘async’ and ‘await’How do I convert an existing callback API to promises?What is the difference between Promises and Observables?Using async/await with a forEach loopis this Promise to async convertion correct and why await is not necessary?
Why am I not getting stuck in the loop
Our group keeps dying during the Lost Mine of Phandelver campaign. What are we doing wrong?
What is the German idiom or expression for when someone is being hypocritical against their own teachings?
What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?
Not been paid even after reminding the Treasurer; what should I do?
Does a humanoid possessed by a ghost register as undead to a paladin's Divine Sense?
Is a switch from R to Python worth it?
How to switch an 80286 from protected to real mode?
How can I use commands with sudo without changing owner of the files?
Would this winged human/angel be able to fly?
If a vampire drinks blood of a sick human, does the vampire get infected?
Which genus do I use for neutral expressions in German?
Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?
How does LIDAR avoid getting confused in an environment being scanned by hundreds of other LIDAR?
Is there a way to prevent the production team from messing up my paper?
Make a living as a math programming freelancer?
Why does putting a dot after the URL remove login information?
How to sort List<T> in c#
How do I get the =LEFT function in excel, to also take the number zero as the first number?
Why do proponents of guns oppose gun competency tests?
3 beeps on a 486 computer with an American Megatrends bios?
How to check a file was encrypted (really & correctly)
How and where to get you research work assessed for PhD?
Ubuntu show wrong disk sizes, how to solve it?
Convert promise function to async/await
Is there an “exists” function for jQuery?How can I convert a string to boolean in JavaScript?var functionName = function() vs function functionName() Set a default parameter value for a JavaScript functionConvert JS object to JSON stringHow and when to use ‘async’ and ‘await’How do I convert an existing callback API to promises?What is the difference between Promises and Observables?Using async/await with a forEach loopis this Promise to async convertion correct and why await is not necessary?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to convert this function to async/await. But got confused. Is there a way to do it?
const csv = require('fast-csv');
const inputFileFolder = 'upload';
const readData = (inputFileName) =>
const csvData = [];
return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;
javascript node.js promise async-await
add a comment |
I am trying to convert this function to async/await. But got confused. Is there a way to do it?
const csv = require('fast-csv');
const inputFileFolder = 'upload';
const readData = (inputFileName) =>
const csvData = [];
return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;
javascript node.js promise async-await
1
what's the question?
– Rafael
Mar 27 at 3:52
1
You can't useasync/awaitwith that - you need to use it within the function that is handling the promise.
– Jack Bashford
Mar 27 at 3:52
2
I guess you couldawait new Promiseinstead ofreturn new Promisebut that doesn't really accomplish anything useful - per @Jack, if you want to useawaitwith it, useawaitin the consumer ofreadData
– CertainPerformance
Mar 27 at 3:54
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
1
Why? "making it async/await" you'll still need to "promisify"csv.fromPath- async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises
– Jaromanda X
Mar 27 at 4:00
add a comment |
I am trying to convert this function to async/await. But got confused. Is there a way to do it?
const csv = require('fast-csv');
const inputFileFolder = 'upload';
const readData = (inputFileName) =>
const csvData = [];
return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;
javascript node.js promise async-await
I am trying to convert this function to async/await. But got confused. Is there a way to do it?
const csv = require('fast-csv');
const inputFileFolder = 'upload';
const readData = (inputFileName) =>
const csvData = [];
return new Promise((resolve, reject) =>
csv
.fromPath(`$inputFileFolder/$inputFileName`)
.on('data', (data) =>
csvData.push(data);
)
.on('end', () =>
resolve(csvData);
)
.on('error', () =>
reject(new Error('Something went wrong while reading the CSV file.'));
);
);
;
javascript node.js promise async-await
javascript node.js promise async-await
edited Mar 27 at 3:55
Shaolin
asked Mar 27 at 3:50
ShaolinShaolin
1,8963 gold badges25 silver badges34 bronze badges
1,8963 gold badges25 silver badges34 bronze badges
1
what's the question?
– Rafael
Mar 27 at 3:52
1
You can't useasync/awaitwith that - you need to use it within the function that is handling the promise.
– Jack Bashford
Mar 27 at 3:52
2
I guess you couldawait new Promiseinstead ofreturn new Promisebut that doesn't really accomplish anything useful - per @Jack, if you want to useawaitwith it, useawaitin the consumer ofreadData
– CertainPerformance
Mar 27 at 3:54
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
1
Why? "making it async/await" you'll still need to "promisify"csv.fromPath- async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises
– Jaromanda X
Mar 27 at 4:00
add a comment |
1
what's the question?
– Rafael
Mar 27 at 3:52
1
You can't useasync/awaitwith that - you need to use it within the function that is handling the promise.
– Jack Bashford
Mar 27 at 3:52
2
I guess you couldawait new Promiseinstead ofreturn new Promisebut that doesn't really accomplish anything useful - per @Jack, if you want to useawaitwith it, useawaitin the consumer ofreadData
– CertainPerformance
Mar 27 at 3:54
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
1
Why? "making it async/await" you'll still need to "promisify"csv.fromPath- async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises
– Jaromanda X
Mar 27 at 4:00
1
1
what's the question?
– Rafael
Mar 27 at 3:52
what's the question?
– Rafael
Mar 27 at 3:52
1
1
You can't use
async/await with that - you need to use it within the function that is handling the promise.– Jack Bashford
Mar 27 at 3:52
You can't use
async/await with that - you need to use it within the function that is handling the promise.– Jack Bashford
Mar 27 at 3:52
2
2
I guess you could
await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData– CertainPerformance
Mar 27 at 3:54
I guess you could
await new Promise instead of return new Promise but that doesn't really accomplish anything useful - per @Jack, if you want to use await with it, use await in the consumer of readData– CertainPerformance
Mar 27 at 3:54
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
1
1
Why? "making it async/await" you'll still need to "promisify"
csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises– Jaromanda X
Mar 27 at 4:00
Why? "making it async/await" you'll still need to "promisify"
csv.fromPath - async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises– Jaromanda X
Mar 27 at 4:00
add a comment |
2 Answers
2
active
oldest
votes
async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.
Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.
add a comment |
You can only use await if you declare it in a async function.
You can use it and then use correct error handling.
But if you want to use await you could use
async function name()
await // Your code here
;
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
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%2f55369523%2fconvert-promise-function-to-async-await%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
async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.
Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.
add a comment |
async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.
Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.
add a comment |
async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.
Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.
async..await provides syntactic sugar for then and catch promise methods to chain promises in synchronous-like manner.
Since the promise is created but not chained, the use of async..await won't improve the code. new Promise is still needed with async..await when there's no promise to chain.
edited Mar 27 at 7:22
answered Mar 27 at 7:17
Estus FlaskEstus Flask
86.8k25 gold badges143 silver badges262 bronze badges
86.8k25 gold badges143 silver badges262 bronze badges
add a comment |
add a comment |
You can only use await if you declare it in a async function.
You can use it and then use correct error handling.
But if you want to use await you could use
async function name()
await // Your code here
;
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
add a comment |
You can only use await if you declare it in a async function.
You can use it and then use correct error handling.
But if you want to use await you could use
async function name()
await // Your code here
;
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
add a comment |
You can only use await if you declare it in a async function.
You can use it and then use correct error handling.
But if you want to use await you could use
async function name()
await // Your code here
;
You can only use await if you declare it in a async function.
You can use it and then use correct error handling.
But if you want to use await you could use
async function name()
await // Your code here
;
answered Mar 27 at 4:08
GyzimoGyzimo
397 bronze badges
397 bronze badges
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
add a comment |
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
I know how to write async function. My problem was converting this particular one.
– Shaolin
Mar 27 at 4:21
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%2f55369523%2fconvert-promise-function-to-async-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
1
what's the question?
– Rafael
Mar 27 at 3:52
1
You can't use
async/awaitwith that - you need to use it within the function that is handling the promise.– Jack Bashford
Mar 27 at 3:52
2
I guess you could
await new Promiseinstead ofreturn new Promisebut that doesn't really accomplish anything useful - per @Jack, if you want to useawaitwith it, useawaitin the consumer ofreadData– CertainPerformance
Mar 27 at 3:54
@JackBashford I do it. But was wondering if I can make this async/await.
– Shaolin
Mar 27 at 3:56
1
Why? "making it async/await" you'll still need to "promisify"
csv.fromPath- async await is not used to make callback style code into Promise style code - is is, as has already been said twice, used for the consumer of Promises– Jaromanda X
Mar 27 at 4:00