JavaScript asynchronous programming: pending states of async/await
and promises
async/await
is an elegant way to handle asynchronous operations in JavaScript, but its behavior is worth exploring in depth when the Promise is in a long-term pending state. This article will analyze the performance of await
when the Promise is not resolve
or reject
.
Scenario Analysis
Consider the following code:
async function yyy() { console.log('yyy calls xxx to start'); let res = await xxx(); console.log('yyy call xxx ends', res); } async function xxx() { return new Promise(function(resolve, reject) { // No resolve or reject call}); } yyy();
The xxx
function returns a promise that is always in the pending state. What happens with await xxx()
in the yyy
function?
Environment Differences: Node.js vs. Browser
The behavior of await
is affected by the operating environment:
Node.js: If Promise remains pending and there are no other active asynchronous operations (such as timers, network requests), the Node.js event loop will consider the program to be completed and exited. It won't wait for the pending Promise indefinitely.
Browser: In a browser environment, await
will usually wait for the Promise to complete. However, if other asynchronous tasks are lacking to drive the event loop, the browser engine may think that the script has been executed and will not block page rendering or interaction. But if the script is executed synchronously (for example, directly

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool