Skip to main content
added 295 characters in body
Source Link
cdhowie
  • 172.3k
  • 25
  • 303
  • 324

You can start the asynchronous operation but not await it yet:

function suppressUnhandledRejections(p) { p.catch(() => {}); return p; } async function main() { // We have to suppress unhandled rejections on these promises. If they become // rejected before we await them later, we'd get a warning otherwise. const servicePromise = suppressUnhandledRejections(GetServices()); this.processPromise = suppressUnhandledRejections(GetProcess()); // Do other stuff const service = await GetServices();servicePromise; const process = await this.processPromise; } 

Also consider using Promise.all() which returns a promise for the completion of all promises passed to it.

async function main() { const [ services, process, somethingElse ] = await Promise.all([ GetServices(), GetProcess(), SomeOtherAsyncOperation(), ]); // Use the results. } 

You can start the asynchronous operation but not await it yet:

async function main() { const servicePromise = GetServices(); this.processPromise = GetProcess(); // Do other stuff const service = await GetServices(); const process = await this.processPromise; } 

Also consider using Promise.all() which returns a promise for the completion of all promises passed to it.

async function main() { const [ services, process, somethingElse ] = await Promise.all([ GetServices(), GetProcess(), SomeOtherAsyncOperation(), ]); // Use the results. } 

You can start the asynchronous operation but not await it yet:

function suppressUnhandledRejections(p) { p.catch(() => {}); return p; } async function main() { // We have to suppress unhandled rejections on these promises. If they become // rejected before we await them later, we'd get a warning otherwise. const servicePromise = suppressUnhandledRejections(GetServices()); this.processPromise = suppressUnhandledRejections(GetProcess()); // Do other stuff const service = await servicePromise; const process = await this.processPromise; } 

Also consider using Promise.all() which returns a promise for the completion of all promises passed to it.

async function main() { const [ services, process, somethingElse ] = await Promise.all([ GetServices(), GetProcess(), SomeOtherAsyncOperation(), ]); // Use the results. } 
Source Link
cdhowie
  • 172.3k
  • 25
  • 303
  • 324

You can start the asynchronous operation but not await it yet:

async function main() { const servicePromise = GetServices(); this.processPromise = GetProcess(); // Do other stuff const service = await GetServices(); const process = await this.processPromise; } 

Also consider using Promise.all() which returns a promise for the completion of all promises passed to it.

async function main() { const [ services, process, somethingElse ] = await Promise.all([ GetServices(), GetProcess(), SomeOtherAsyncOperation(), ]); // Use the results. }