(async () => )(); what is this?(async () => )(); vs callbackWhat is the most efficient way to deep clone an object in JavaScript?What is the scope of variables in JavaScript?What is the !! (not not) operator in JavaScript?What is the JavaScript version of sleep()?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the 'new' keyword in JavaScript?What is the difference between call and apply?What is JSONP, and why was it created?How and when to use ‘async’ and ‘await’Call async/await functions in parallel
Which dice game has a board with 9x9 squares that has different colors on the diagonals and midway on some edges?
Why is there an extra "t" in Lemmatization?
How to pass array of values in lualatex?
My current job follows "worst practices". How can I talk about my experience in an interview without giving off red flags?
Can a creature sustain itself by eating its own severed body parts?
MITM on HTTPS traffic in Kazakhstan 2019
Killing a star safely
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
Is it possible to pass generic lambda as non-template argument
Is there an English word to describe when a sound "protrudes"?
1025th term of the given sequence.
Are there foods that astronauts are explicitly never allowed to eat?
Why does the salt in the oceans not sink to the bottom?
Why is the UH-60 tail rotor canted?
How can I deal with someone that wants to kill something that isn't supposed to be killed?
I have a domain, static IP and many devices I'd like to access outside my house. How to route them?
What kind of curve (or model) should I fit to my percentage data?
Oriented vector bundle with odd-dimensional fibers
Has Iron Man made any suit for underwater combat?
Is it better to merge "often" or only after completion do a big merge of feature branches?
Considerations when providing money to only one child out of two
Does switching on an old games console without a cartridge damage it?
Hats Question: Confusion Over Its Formulation
Piece of fabric in planter, how to use it?
(async () => )(); what is this?
(async () => )(); vs callbackWhat is the most efficient way to deep clone an object in JavaScript?What is the scope of variables in JavaScript?What is the !! (not not) operator in JavaScript?What is the JavaScript version of sleep()?What does “use strict” do in JavaScript, and what is the reasoning behind it?What is the 'new' keyword in JavaScript?What is the difference between call and apply?What is JSONP, and why was it created?How and when to use ‘async’ and ‘await’Call async/await functions in parallel
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
async function test()
(async () =>
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
)();
What does it mean to put methods (test1,test2,test3) inside async () => )()
?
I find it faster than
async function test()
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
Any downside of using it?
javascript async-await
add a comment |
async function test()
(async () =>
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
)();
What does it mean to put methods (test1,test2,test3) inside async () => )()
?
I find it faster than
async function test()
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
Any downside of using it?
javascript async-await
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically areturn Promise.resolve();
in the first case, which isn't useful at all.
– Patrick Roberts
Jul 12 at 20:14
if it's not useful at all, when should one use(async () => )();
?
– bbusdriver
Jul 12 at 20:36
At the top level, when you want access toasync
/await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.
– Patrick Roberts
Jul 12 at 20:39
add a comment |
async function test()
(async () =>
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
)();
What does it mean to put methods (test1,test2,test3) inside async () => )()
?
I find it faster than
async function test()
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
Any downside of using it?
javascript async-await
async function test()
(async () =>
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
)();
What does it mean to put methods (test1,test2,test3) inside async () => )()
?
I find it faster than
async function test()
var a = await this.test1();
var b = await this.test2(a);
var c = await this.test3(b);
this.doThis(a,b,c);
Any downside of using it?
javascript async-await
javascript async-await
edited Jul 12 at 20:20
Li357
36.2k8 gold badges75 silver badges99 bronze badges
36.2k8 gold badges75 silver badges99 bronze badges
asked Jul 12 at 20:04
bbusdriverbbusdriver
5591 gold badge5 silver badges26 bronze badges
5591 gold badge5 silver badges26 bronze badges
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically areturn Promise.resolve();
in the first case, which isn't useful at all.
– Patrick Roberts
Jul 12 at 20:14
if it's not useful at all, when should one use(async () => )();
?
– bbusdriver
Jul 12 at 20:36
At the top level, when you want access toasync
/await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.
– Patrick Roberts
Jul 12 at 20:39
add a comment |
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically areturn Promise.resolve();
in the first case, which isn't useful at all.
– Patrick Roberts
Jul 12 at 20:14
if it's not useful at all, when should one use(async () => )();
?
– bbusdriver
Jul 12 at 20:36
At the top level, when you want access toasync
/await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.
– Patrick Roberts
Jul 12 at 20:39
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically a
return Promise.resolve();
in the first case, which isn't useful at all.– Patrick Roberts
Jul 12 at 20:14
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically a
return Promise.resolve();
in the first case, which isn't useful at all.– Patrick Roberts
Jul 12 at 20:14
if it's not useful at all, when should one use
(async () => )();
?– bbusdriver
Jul 12 at 20:36
if it's not useful at all, when should one use
(async () => )();
?– bbusdriver
Jul 12 at 20:36
At the top level, when you want access to
async
/ await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.– Patrick Roberts
Jul 12 at 20:39
At the top level, when you want access to
async
/ await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.– Patrick Roberts
Jul 12 at 20:39
add a comment |
1 Answer
1
active
oldest
votes
Both return a promise but they return different promises.
The first will return a promise that may resolve before this.test1()
's result resolves.
The second returns a promise that only resolves after the final call to this.doThis(a,b,c);
.
This has been called the "fire and forget pattern":
Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.
You can see this in
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
Thanks. still not so sure when should one use(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolvesthis.doThis(a,b,c);
even before it resolvesthis.test1();
, doesn't it mean thatthis.doThis(a,b,c);
would error out?
– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by theawait
statements.
– Mike Samuel
Jul 12 at 20:45
|
show 3 more comments
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%2f57013440%2fasync-what-is-this%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
Both return a promise but they return different promises.
The first will return a promise that may resolve before this.test1()
's result resolves.
The second returns a promise that only resolves after the final call to this.doThis(a,b,c);
.
This has been called the "fire and forget pattern":
Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.
You can see this in
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
Thanks. still not so sure when should one use(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolvesthis.doThis(a,b,c);
even before it resolvesthis.test1();
, doesn't it mean thatthis.doThis(a,b,c);
would error out?
– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by theawait
statements.
– Mike Samuel
Jul 12 at 20:45
|
show 3 more comments
Both return a promise but they return different promises.
The first will return a promise that may resolve before this.test1()
's result resolves.
The second returns a promise that only resolves after the final call to this.doThis(a,b,c);
.
This has been called the "fire and forget pattern":
Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.
You can see this in
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
Thanks. still not so sure when should one use(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolvesthis.doThis(a,b,c);
even before it resolvesthis.test1();
, doesn't it mean thatthis.doThis(a,b,c);
would error out?
– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by theawait
statements.
– Mike Samuel
Jul 12 at 20:45
|
show 3 more comments
Both return a promise but they return different promises.
The first will return a promise that may resolve before this.test1()
's result resolves.
The second returns a promise that only resolves after the final call to this.doThis(a,b,c);
.
This has been called the "fire and forget pattern":
Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.
You can see this in
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
Both return a promise but they return different promises.
The first will return a promise that may resolve before this.test1()
's result resolves.
The second returns a promise that only resolves after the final call to this.doThis(a,b,c);
.
This has been called the "fire and forget pattern":
Often in application development you want a process to call another thread and continue the process flow, without waiting for a response from the called thread. This pattern is called the “fire and forget” pattern.
You can see this in
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
function logEventually(str)
return new Promise((resolve) =>
setTimeout(() =>
console.log(str);
resolve(null);
, 0);
);
async function a()
await logEventually('in a 1');
await logEventually('in a 2');
await logEventually('in a 3');
return await logEventually('end of a');
async function b()
(async () =>
await logEventually('in b 1');
await logEventually('in b 2');
await logEventually('in b 3');
)();
return await logEventually('end of b');
a();
b();
edited Jul 12 at 20:42
answered Jul 12 at 20:08
Mike SamuelMike Samuel
96.4k24 gold badges175 silver badges219 bronze badges
96.4k24 gold badges175 silver badges219 bronze badges
Thanks. still not so sure when should one use(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolvesthis.doThis(a,b,c);
even before it resolvesthis.test1();
, doesn't it mean thatthis.doThis(a,b,c);
would error out?
– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by theawait
statements.
– Mike Samuel
Jul 12 at 20:45
|
show 3 more comments
Thanks. still not so sure when should one use(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolvesthis.doThis(a,b,c);
even before it resolvesthis.test1();
, doesn't it mean thatthis.doThis(a,b,c);
would error out?
– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by theawait
statements.
– Mike Samuel
Jul 12 at 20:45
Thanks. still not so sure when should one use
(async () => )();
– bbusdriver
Jul 12 at 20:37
Thanks. still not so sure when should one use
(async () => )();
– bbusdriver
Jul 12 at 20:37
So if it resolves
this.doThis(a,b,c);
even before it resolves this.test1();
, doesn't it mean that this.doThis(a,b,c);
would error out?– bbusdriver
Jul 12 at 20:40
So if it resolves
this.doThis(a,b,c);
even before it resolves this.test1();
, doesn't it mean that this.doThis(a,b,c);
would error out?– bbusdriver
Jul 12 at 20:40
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
@bbusdriver I added a note about the "fire and forget" pattern. I think it fits what's going on in your example.
– Mike Samuel
Jul 12 at 20:42
1
1
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
@MikeSamuel in regards to the "fire and forget" pattern, isn't the outer function generally synchronous? There's no reason for it to return a promise that doesn't actually wait for anything.
– Patrick Roberts
Jul 12 at 20:44
1
1
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by the
await
statements.– Mike Samuel
Jul 12 at 20:45
@bbusdriver. The promise returned by the whole may resolve before test1 resolves. The chain of awaits within the inner lambda still proceed in the order implied by the
await
statements.– Mike Samuel
Jul 12 at 20:45
|
show 3 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f57013440%2fasync-what-is-this%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
It just seems faster because the returned promise doesn't doesn't actually wait for any of your sequential logic. You might as well change it to a synchronous function since the returned promise is basically a
return Promise.resolve();
in the first case, which isn't useful at all.– Patrick Roberts
Jul 12 at 20:14
if it's not useful at all, when should one use
(async () => )();
?– bbusdriver
Jul 12 at 20:36
At the top level, when you want access to
async
/await
syntax. If that expression exists within another function, it means the caller of that function will not be able to know when the asynchronous calls have completed.– Patrick Roberts
Jul 12 at 20:39