6310

serial await requests in a return run in parallel?

Having a discussion with someone and came across this oddity:

const wait = async () => new Promise(resolve => setTimeout(resolve, 1000)); async function case1() { const {a, b} = {a: await wait(), b: await wait()}; return {a, b}; } async function case2() { return {a: await wait(), b: await wait()}; } async function case3() { const {a, b} = {a: wait(), b: wait()}; return {a: await a, b: await b}; } async function case4() { const {a, b} = {a: wait(), b: wait()}; const {c, d} = {c: await a, d: await b}; return {c, d}; } function test() { const start = new Date(); case1().then(() => console.log('case1:', +new Date() - start)); case2().then(() => console.log('case2:', +new Date() - start)); case3().then(() => console.log('case3:', +new Date() - start)); case4().then(() => console.log('case4:', +new Date() - start)); }

case1 and case2 both run in 2 seconds. case3 and case4 run in 1 second.

Is there some weird implicit Promise.all or something??

Answer1:

You call the function wait() without utilizing await at case3 and case4. That is the difference.

Answer2:

In case#3 the wait() functions are called immediately, so there is only 1 second of timeout (for both of them), while in the other two (case#1 and case#2) the await will "do it's job" and wait for the async call to return.

As you can see here, the console.log(Date()) is being called immediately for both calls. <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

const wait = async () => new Promise(resolve => console.log(Date()) || setTimeout(resolve, 1000));

async function case3() {
  const {a, b} = {a: wait(), b: wait()};
  return {a: await a, b: await b};
}

function test() {
  const start = new Date();
  case3().then(() => console.log('case3:', +new Date() - start));
}
test();


And here it is being synchronized using the await:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

const wait = async () => new Promise(resolve => console.log(Date()) || setTimeout(resolve, 1000));

async function case1() {
  const {a, b} = {a: await wait(), b: await wait()};
  return {a, b};
}


function test() {
  const start = new Date();

  case1().then(() => console.log('case1:', +new Date() - start));
}
test();


Recommend

  • While/Switch Statement weird output
  • Android: Checking one CheckBox checks all CheckBoxes in the same row
  • overload + operator to add 2 polynomials C++ [duplicate]
  • map expression in case clause in scala pattern matching
  • check if there is a node but not text
  • Displaying multiple xml views w/o fragments
  • How to work around issue with ambiguity when monomorphic restriction turned *on*?
  • What is the overhead of using a dictionary instead of a list?
  • F# comparing discriminated unions' by case identifier
  • Compare enums with associated values in Swift
  • Get Users in Group from Azure AD via Microsoft Graph
  • why my app based on boost::asio didn't accept new connection
  • Opaque reference instead of PImpl. Is it possible?
  • Delphi: Where is the shortcut that started the application? [duplicate]
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Authentication failed with Azure Active Directory in Windows Phone
  • How do I mock an exported typescript function in a jasmine test?
  • ThreadStatic in asynchronous ASP.NET Web API
  • Invalid Date on validation Date of js
  • C: Incompatible pointer type initializing
  • Allowing both email and username for authentication
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Android Google Maps API OnLocationChanged only called once
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?