"use strict";
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
{
console.log('=== Sample promise chain with .then() and .catch() ===');
function doSomething() {
console.log('Starting doSomething');
return new Promise((resolve) => {
setTimeout(() => resolve(1), 10);
});
}
function doSomethingElse(result) {
console.log('Starting doSomethingElse with', result);
return new Promise((resolve) => {
setTimeout(() => resolve(result + 2), 10);
});
}
function doThirdThing(result) {
console.log('Starting doThirdThing with', result);
return new Promise((resolve) => {
setTimeout(() => resolve(result * 3), 10);
});
}
function failureCallback(err) {
console.log('Chain failed:', err);
}
doSomething()
.then((result) => doSomethingElse(result))
.then((newResult) => doThirdThing(newResult))
.then((finalResult) => {
console.log(`Got the final result: ${finalResult}`);
})
.catch(failureCallback);
try {
assert(false, 'Execution continues after promise chain');
} catch (e) {
console.log('Caught error:', e);
}
}
{
console.log('=== Sample promise chain with async/await ===');
function doSomething() {
console.log('[async/await] Starting doSomething');
return new Promise((resolve) => {
setTimeout(() => resolve(2), 10);
});
}
function doSomethingElse(result) {
console.log('[async/await] Starting doSomethingElse with', result);
return new Promise((resolve) => {
setTimeout(() => resolve(result + 2), 10);
});
}
function doThirdThing(result) {
console.log('[async/await] Starting doThirdThing with', result);
return new Promise((resolve) => {
setTimeout(() => resolve(result * 3), 10);
});
}
function failureCallback(err) {
console.log('[async/await] Chain failed (global):', err);
}
async function foo() {
try {
const result = await doSomething();
const newResult = await doSomethingElse(result);
const finalResult = await doThirdThing(newResult);
console.log(`[async/await] Got the final result: ${finalResult}`);
} catch (error) {
failureCallback(error);
}
}
foo().catch(failureCallback);
}
{
console.log('=== Sample promise chain with optional steps ===');
function doSomethingCritical() {
console.log('[control flow] Starting critical work === 1 ===');
return new Promise((resolve, reject) => {
setTimeout(() => resolve('crit-ok'), 10);
});
}
function doSomethingOptional() {
console.log('[control flow] Starting optional work === 2 ===');
return new Promise((resolve, reject) => {
console.log('[debug] typeof Error =', typeof Error);
if (Math.random() < 0.5) {
setTimeout(() => resolve('opt-result'), 10);
} else {
setTimeout(() => reject(new Error('[control flow] optional failed')), 10);
}
}).catch(e => {
console.log(`[control flow] optional failed (internal): ${e.message}`);
return undefined;
});
}
function doSomethingExtraNice(optionalResult) {
console.log('[control flow] Starting extra nice work === 3 === with', optionalResult);
return new Promise((resolve) => {
setTimeout(() => resolve(`extra-${optionalResult}`), 10);
});
}
function moreCriticalStuff() {
console.log('[control flow] Doing more critical work === 4 ===');
return new Promise((resolve) => setTimeout(() => resolve('all-done'), 10));
}
doSomethingCritical()
.then((result) =>
doSomethingOptional()
.then((optionalResult) => doSomethingExtraNice(optionalResult))
.catch((e) => { console.log(`[control flow] optional failed === 3.5 ===: ${e.message}`); }),
) .then(() => moreCriticalStuff())
.catch((e) => console.log(`[control flow] 严重失败 === 5 ===: ${e.message}`));
async function main() {
try {
const result = await doSomethingCritical();
try {
const optionalResult = await doSomethingOptional(result);
await doSomethingExtraNice(optionalResult);
} catch (e) {
console.log(`[control flow] optional failed (async/await) === 3.5 ===: ${e.message}`);
}
await moreCriticalStuff();
} catch (e) {
console.error(`[control flow] 严重失败 (async/await) === 5 ===: ${e.message}`);
}
}
main();
}
{
console.log('=== Sample promise chain demonstrating .then() after .catch() ===');
function doSomething() {
console.log('Starting doSomething for .then() after .catch() example');
return new Promise((resolve) => {
setTimeout(() => resolve(), 10);
});
}
doSomething()
.then(() => {
throw new Error("Something failed");
console.log("Do this");
})
.catch(() => {
console.error("Do that");
})
.then(() => {
console.log("Do this, no matter what happened before");
});
async function main() {
try {
await doSomething();
throw new Error("Something failed");
console.log("Do this");
} catch (e) {
console.error("Do that");
}
console.log("Do this, no matter what happened before");
}
main();
}
{
console.log('=== Sample promise chain demonstrating execution order ===');
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
wait().then(() => console.log(4));
Promise.resolve()
.then(() => console.log(2))
.then(() => console.log(3));
console.log(1); }
{
const promise = new Promise((resolve, reject) => {
console.log("Promise callback");
resolve();
}).then((result) => {
console.log("Promise callback (.then)");
});
setTimeout(() => {
console.log("event-loop cycle: Promise (fulfilled)", promise);
}, 0);
console.log("Promise (pending)", promise);
}