Open-Source JavaScript Programming • Foundational • Estimated study time: 45 minutes.
A monitoring service may request readings from several independent devices. One device may fail while the others return usable data. JavaScript promises represent the eventual outcomes of asynchronous operations, while async and await provide readable syntax for consuming those outcomes. This lesson builds on functions and callbacks.
Learning objectives
Recognize pending, fulfilled and rejected promises; distinguish Promise.all from Promise.allSettled; use async and await with error handling; calculate a mean without treating failed requests as zero; explain why a promise does not automatically create a separate execution thread.
Tutorial: Understanding Promises and Async Await in JavaScript, including promise-chain error propagation.
Promise behavior and concurrency
An async function returns a promise. An await expression suspends that function’s continuation until the awaited value settles; it does not block the entire JavaScript runtime. A rejection becomes a thrown exception at the await point. Promise.all fulfills when all inputs fulfill and rejects on the first observed rejection; it does not cancel work already started. Promise.allSettled waits for every outcome and returns an ordered array of status records. Read MDN’s promise guide. CPU-heavy synchronous code remains blocking unless moved to another execution context.

Worked example: partial telemetry failure
This simulation reuses array operations, object properties and conditional control flow. The simulated sensor B rejects. Valid readings A and C are 24 and 26 °C. The mean is T̄ = Σ Ti / n, where Ti is a valid measurement and n is its count. Here n = 2 and T̄ = 25 °C. A missing reading is not 0 °C.
import assert from 'node:assert/strict';
async function readSensor(id) {
if (id === 'B') throw new Error('offline');
return id === 'A' ? 24 : 26;
}
async function report(ids) {
const results = await Promise.allSettled(ids.map(readSensor));
const valid = results.filter(r => r.status === 'fulfilled').map(r => r.value);
return { valid: valid.length, failed: results.length - valid.length,
mean: valid.length ? valid.reduce((a,b) => a+b,0)/valid.length : null };
}
assert.deepEqual(await report(['A','B','C']), {valid:2,failed:1,mean:25});
assert.deepEqual(await report(['B']), {valid:0,failed:1,mean:null});
assert.deepEqual(await report([]), {valid:0,failed:0,mean:null});
await assert.rejects(Promise.all(['A','B'].map(readSensor)), /offline/);
console.log(JSON.stringify(await report(['A','B','C'])));
Save as telemetry.mjs. Run node –check telemetry.mjs and node telemetry.mjs. Node.js v22.16.0 passed syntax checking and all four assertions. Actual output:
{"valid":2,"failed":1,"mean":25}
The community discussion illustrates why await inside a forEach callback does not make the outer function wait. Use a for…of loop for intentional sequential awaits, or map and an appropriate promise combinator for independent requests.
Operational considerations
This is a deterministic simulation, not actual equipment I/O. Real requests may never settle, so set timeouts and use AbortController where the underlying API supports cancellation. Limit concurrency for large device fleets; Promise.allSettled does not impose a concurrency limit. Preserve timestamps, failed-device identifiers and error causes in production. Do not derive safety-critical decisions from a partial mean without an explicit policy.
Exercises and answers
A. What does an async function return? B. Does Promise.all cancel other inputs when one rejects? C. Why does a missing reading not enter the denominator? D. What result is returned for an empty device list? E. Which combinator preserves individual outcomes? F. Does await block all JavaScript execution?
Answers: A. A promise. B. No. C. It is not an observed measurement. D. valid=0, failed=0, mean=null. E. Promise.allSettled. F. No; it suspends its surrounding async continuation. Further reading: Promise.allSettled reference.
Featured artwork is conceptual editorial illustration, not a screenshot of the example.
Editor’s Note: We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiatives, please scan the QR code below

For mobile scanning, open the article on another display and scan the QR code with a Bitcoin wallet. The voluntary donation amount is 5,000 sats. Verify the receiving address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb before sending.
BitcoinVersus.Tech publishes technology and financial journalism for informational purposes and does not provide financial, investment, or legal advice.

Leave a Reply