-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpromise-methods.js
More file actions
700 lines (557 loc) · 19.1 KB
/
promise-methods.js
File metadata and controls
700 lines (557 loc) · 19.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/**
* Promise Methods and Async Programming
*
* Description:
* This file demonstrates Promise methods, async/await, and various patterns
* for handling asynchronous operations in JavaScript.
*/
// ========================================
// BASIC PROMISE CREATION AND METHODS
// ========================================
console.log("=== Basic Promise Methods ===\n");
// Example 1: Promise.resolve() and Promise.reject()
console.log("Example 1: Promise.resolve() and Promise.reject()");
const resolvedPromise = Promise.resolve("Success!");
const rejectedPromise = Promise.reject(new Error("Failure!"));
resolvedPromise
.then(value => console.log("Resolved:", value))
.catch(error => console.log("Error:", error.message));
rejectedPromise
.then(value => console.log("Resolved:", value))
.catch(error => console.log("Rejected:", error.message));
// Creating promises from non-promise values
const numberPromise = Promise.resolve(42);
const arrayPromise = Promise.resolve([1, 2, 3]);
Promise.all([numberPromise, arrayPromise])
.then(([number, array]) => {
console.log("Number:", number);
console.log("Array:", array);
});
console.log();
// Example 2: Basic Promise constructor
console.log("Example 2: Promise constructor");
function delay(ms) {
return new Promise(resolve => {
setTimeout(() => resolve(`Waited ${ms}ms`), ms);
});
}
function randomSuccess(probability = 0.5) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < probability) {
resolve("Operation succeeded!");
} else {
reject(new Error("Operation failed!"));
}
}, 1000);
});
}
delay(500)
.then(message => {
console.log(message);
return "Next step";
})
.then(nextMessage => console.log(nextMessage));
randomSuccess(0.8)
.then(result => console.log("Random success:", result))
.catch(error => console.log("Random failure:", error.message));
console.log();
// ========================================
// PROMISE STATIC METHODS
// ========================================
console.log("=== Promise Static Methods ===\n");
// Example 3: Promise.all() - Wait for all promises
console.log("Example 3: Promise.all()");
const promise1 = delay(1000).then(() => "First");
const promise2 = delay(800).then(() => "Second");
const promise3 = delay(1200).then(() => "Third");
Promise.all([promise1, promise2, promise3])
.then(results => {
console.log("Promise.all results:", results);
console.log("All promises completed");
})
.catch(error => {
console.log("Promise.all error:", error.message);
});
// Example with one rejection
const promiseWithError = Promise.reject(new Error("Something went wrong"));
Promise.all([delay(500), promiseWithError, delay(1000)])
.then(results => console.log("This won't execute"))
.catch(error => console.log("Promise.all failed fast:", error.message));
console.log();
// Example 4: Promise.allSettled() - Wait for all to settle (ES2020)
console.log("Example 4: Promise.allSettled()");
const mixedPromises = [
Promise.resolve("Success 1"),
Promise.reject(new Error("Error 1")),
delay(300).then(() => "Success 2"),
Promise.reject(new Error("Error 2"))
];
Promise.allSettled(mixedPromises)
.then(results => {
console.log("Promise.allSettled results:");
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
console.log(` ${index}: Success - ${result.value}`);
} else {
console.log(` ${index}: Failed - ${result.reason.message}`);
}
});
});
console.log();
// Example 5: Promise.race() - First to settle wins
console.log("Example 5: Promise.race()");
const fastPromise = delay(300).then(() => "Fast");
const slowPromise = delay(1000).then(() => "Slow");
const failingPromise = delay(500).then(() => { throw new Error("Failed"); });
Promise.race([fastPromise, slowPromise])
.then(result => console.log("Promise.race winner:", result));
Promise.race([slowPromise, failingPromise])
.then(result => console.log("Won't execute"))
.catch(error => console.log("Promise.race failed:", error.message));
console.log();
// Example 6: Promise.any() - First successful promise (ES2021)
console.log("Example 6: Promise.any()");
const failFast = Promise.reject(new Error("Fail fast"));
const failSlow = delay(800).then(() => { throw new Error("Fail slow"); });
const succeedLater = delay(1200).then(() => "Success!");
Promise.any([failFast, failSlow, succeedLater])
.then(result => console.log("Promise.any result:", result))
.catch(error => console.log("All promises failed:", error.message));
// All failing promises
Promise.any([
Promise.reject(new Error("Error 1")),
Promise.reject(new Error("Error 2")),
Promise.reject(new Error("Error 3"))
])
.then(result => console.log("Won't execute"))
.catch(error => {
console.log("Promise.any all failed:", error.name); // AggregateError
console.log("Individual errors:", error.errors.map(e => e.message));
});
console.log();
// ========================================
// ASYNC/AWAIT
// ========================================
console.log("=== Async/Await ===\n");
// Example 7: Basic async/await
console.log("Example 7: Basic async/await");
async function fetchUserData(userId) {
try {
console.log(`Fetching user ${userId}...`);
await delay(500);
if (userId <= 0) {
throw new Error("Invalid user ID");
}
return {
id: userId,
name: `User ${userId}`,
email: `user${userId}@example.com`
};
} catch (error) {
console.log("Error fetching user:", error.message);
throw error;
}
}
async function demonstrateAsyncAwait() {
try {
const user1 = await fetchUserData(1);
console.log("User 1:", user1);
const user2 = await fetchUserData(2);
console.log("User 2:", user2);
// This will throw an error
const invalidUser = await fetchUserData(-1);
console.log("This won't execute");
} catch (error) {
console.log("Async function error:", error.message);
}
}
demonstrateAsyncAwait();
console.log();
// Example 8: Parallel execution with async/await
console.log("Example 8: Parallel execution");
async function sequentialExecution() {
console.time("Sequential");
const result1 = await delay(300).then(() => "First");
const result2 = await delay(300).then(() => "Second");
const result3 = await delay(300).then(() => "Third");
console.timeEnd("Sequential");
return [result1, result2, result3];
}
async function parallelExecution() {
console.time("Parallel");
const promises = [
delay(300).then(() => "First"),
delay(300).then(() => "Second"),
delay(300).then(() => "Third")
];
const results = await Promise.all(promises);
console.timeEnd("Parallel");
return results;
}
// Run both approaches
sequentialExecution().then(results =>
console.log("Sequential results:", results)
);
parallelExecution().then(results =>
console.log("Parallel results:", results)
);
console.log();
// ========================================
// ERROR HANDLING PATTERNS
// ========================================
console.log("=== Error Handling Patterns ===\n");
// Example 9: Promise error handling
console.log("Example 9: Promise error handling");
function unreliableAPI(shouldFail = false) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (shouldFail) {
reject(new Error("API Error"));
} else {
resolve("API Success");
}
}, 300);
});
}
// Chain error handling
unreliableAPI(false)
.then(result => {
console.log("Success:", result);
return unreliableAPI(true); // This will fail
})
.then(result => {
console.log("This won't execute");
})
.catch(error => {
console.log("Chain error:", error.message);
return "Recovered"; // Recovery
})
.then(result => {
console.log("After recovery:", result);
});
// Multiple catch blocks
unreliableAPI(true)
.catch(error => {
if (error.message === "API Error") {
console.log("Specific API error handled");
throw new Error("Rethrown error");
}
return "Default recovery";
})
.catch(error => {
console.log("Second catch:", error.message);
});
console.log();
// Example 10: Async/await error handling
console.log("Example 10: Async/await error handling");
async function robustAsyncFunction() {
try {
const result1 = await unreliableAPI(false);
console.log("First call:", result1);
const result2 = await unreliableAPI(true);
console.log("This won't execute");
} catch (error) {
console.log("Async error caught:", error.message);
// You can still use try/catch for specific operations
try {
const recovery = await unreliableAPI(false);
console.log("Recovery successful:", recovery);
return recovery;
} catch (recoveryError) {
console.log("Recovery failed:", recoveryError.message);
throw new Error("Complete failure");
}
}
}
robustAsyncFunction()
.then(result => console.log("Final result:", result))
.catch(error => console.log("Unhandled error:", error.message));
console.log();
// ========================================
// ADVANCED PROMISE PATTERNS
// ========================================
console.log("=== Advanced Promise Patterns ===\n");
// Example 11: Promise retry pattern
console.log("Example 11: Promise retry pattern");
function promiseRetry(promiseFactory, maxRetries = 3, delayMs = 1000) {
return new Promise(async (resolve, reject) => {
let lastError;
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
const result = await promiseFactory();
resolve(result);
return;
} catch (error) {
lastError = error;
console.log(`Attempt ${attempt} failed:`, error.message);
if (attempt < maxRetries) {
await delay(delayMs);
}
}
}
reject(lastError);
});
}
// Usage
promiseRetry(() => randomSuccess(0.3), 3, 500)
.then(result => console.log("Retry success:", result))
.catch(error => console.log("Retry failed:", error.message));
console.log();
// Example 12: Promise timeout pattern
console.log("Example 12: Promise timeout pattern");
function promiseTimeout(promise, timeoutMs) {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => reject(new Error(`Timeout after ${timeoutMs}ms`)), timeoutMs);
});
return Promise.race([promise, timeoutPromise]);
}
// Usage
const slowOperation = delay(2000).then(() => "Slow result");
promiseTimeout(slowOperation, 1000)
.then(result => console.log("Timeout success:", result))
.catch(error => console.log("Timeout error:", error.message));
console.log();
// Example 13: Promise queue for rate limiting
console.log("Example 13: Promise queue");
class PromiseQueue {
constructor(concurrency = 1) {
this.concurrency = concurrency;
this.running = 0;
this.queue = [];
}
add(promiseFactory) {
return new Promise((resolve, reject) => {
this.queue.push({
promiseFactory,
resolve,
reject
});
this.process();
});
}
async process() {
if (this.running >= this.concurrency || this.queue.length === 0) {
return;
}
this.running++;
const { promiseFactory, resolve, reject } = this.queue.shift();
try {
const result = await promiseFactory();
resolve(result);
} catch (error) {
reject(error);
} finally {
this.running--;
this.process(); // Process next item
}
}
}
const queue = new PromiseQueue(2); // Max 2 concurrent operations
// Add multiple tasks
for (let i = 1; i <= 5; i++) {
queue.add(() => delay(500).then(() => `Task ${i} completed`))
.then(result => console.log(result));
}
console.log();
// Example 14: Promise-based event emitter
console.log("Example 14: Promise-based events");
class PromiseEventEmitter {
constructor() {
this.events = {};
}
on(event, listener) {
if (!this.events[event]) {
this.events[event] = [];
}
this.events[event].push(listener);
}
emit(event, data) {
if (this.events[event]) {
this.events[event].forEach(listener => listener(data));
}
}
once(event) {
return new Promise(resolve => {
const listener = (data) => {
resolve(data);
// Remove listener after first call
const index = this.events[event].indexOf(listener);
if (index > -1) {
this.events[event].splice(index, 1);
}
};
this.on(event, listener);
});
}
}
const emitter = new PromiseEventEmitter();
// Wait for event
emitter.once('data')
.then(data => console.log("Received data:", data));
// Emit event after delay
setTimeout(() => {
emitter.emit('data', { message: "Hello from event!" });
}, 1000);
console.log();
// ========================================
// PRACTICAL ASYNC PATTERNS
// ========================================
console.log("=== Practical Async Patterns ===\n");
// Example 15: Async data fetching with caching
console.log("Example 15: Async caching");
class AsyncCache {
constructor(ttl = 5000) {
this.cache = new Map();
this.ttl = ttl;
}
async get(key, fetcher) {
const cached = this.cache.get(key);
if (cached && (Date.now() - cached.timestamp) < this.ttl) {
console.log(`Cache hit for ${key}`);
return cached.data;
}
console.log(`Cache miss for ${key}, fetching...`);
const data = await fetcher();
this.cache.set(key, {
data,
timestamp: Date.now()
});
return data;
}
clear() {
this.cache.clear();
}
}
const cache = new AsyncCache(3000);
async function fetchUser(id) {
await delay(500); // Simulate API call
return { id, name: `User ${id}`, timestamp: Date.now() };
}
// Usage
async function demonstrateCache() {
console.log("First fetch:");
const user1 = await cache.get('user1', () => fetchUser(1));
console.log(user1);
console.log("Second fetch (should hit cache):");
const user1Again = await cache.get('user1', () => fetchUser(1));
console.log(user1Again);
}
demonstrateCache();
console.log();
// Example 16: Async iterator and generator
console.log("Example 16: Async iterators");
async function* asyncNumberGenerator(max) {
for (let i = 1; i <= max; i++) {
await delay(200); // Simulate async operation
yield i;
}
}
async function consumeAsyncIterator() {
console.log("Consuming async iterator:");
for await (const number of asyncNumberGenerator(5)) {
console.log("Generated:", number);
}
console.log("Iterator complete");
}
consumeAsyncIterator();
// Example 17: Batch processing
console.log("Example 17: Batch processing");
async function processBatch(items, batchSize, processor) {
const results = [];
for (let i = 0; i < items.length; i += batchSize) {
const batch = items.slice(i, i + batchSize);
console.log(`Processing batch ${Math.floor(i / batchSize) + 1}:`, batch);
const batchResults = await Promise.all(
batch.map(item => processor(item))
);
results.push(...batchResults);
// Optional delay between batches
if (i + batchSize < items.length) {
await delay(100);
}
}
return results;
}
async function processItem(item) {
await delay(Math.random() * 300);
return item * 2;
}
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
processBatch(items, 3, processItem)
.then(results => console.log("Batch results:", results));
console.log();
// ========================================
// ERROR RECOVERY PATTERNS
// ========================================
console.log("=== Error Recovery Patterns ===\n");
// Example 18: Circuit breaker pattern
console.log("Example 18: Circuit breaker");
class CircuitBreaker {
constructor(threshold = 3, timeout = 5000) {
this.failureThreshold = threshold;
this.timeout = timeout;
this.failureCount = 0;
this.lastFailureTime = null;
this.state = 'CLOSED'; // CLOSED, OPEN, HALF_OPEN
}
async call(operation) {
if (this.state === 'OPEN') {
if (Date.now() - this.lastFailureTime >= this.timeout) {
this.state = 'HALF_OPEN';
} else {
throw new Error('Circuit breaker is OPEN');
}
}
try {
const result = await operation();
this.onSuccess();
return result;
} catch (error) {
this.onFailure();
throw error;
}
}
onSuccess() {
this.failureCount = 0;
this.state = 'CLOSED';
}
onFailure() {
this.failureCount++;
this.lastFailureTime = Date.now();
if (this.failureCount >= this.failureThreshold) {
this.state = 'OPEN';
}
}
}
const circuitBreaker = new CircuitBreaker(2, 2000);
async function unreliableService() {
if (Math.random() < 0.7) {
throw new Error('Service failure');
}
return 'Service success';
}
// Test circuit breaker
async function testCircuitBreaker() {
for (let i = 1; i <= 6; i++) {
try {
const result = await circuitBreaker.call(unreliableService);
console.log(`Call ${i}: ${result}`);
} catch (error) {
console.log(`Call ${i}: ${error.message}`);
}
await delay(500);
}
}
testCircuitBreaker();
console.log("\n=== Best Practices ===");
console.log("1. Use Promise.all() for parallel operations, await for sequential");
console.log("2. Use Promise.allSettled() when you need all results regardless of failures");
console.log("3. Implement proper error handling with try/catch or .catch()");
console.log("4. Use Promise.race() for timeouts and competitive operations");
console.log("5. Avoid mixing callbacks with Promises - promisify when needed");
console.log("6. Use async/await for cleaner, more readable asynchronous code");
console.log("7. Implement retry logic for transient failures");
console.log("8. Use circuit breakers for external service calls");
console.log("9. Cache async results to improve performance");
console.log("10. Consider memory leaks when storing promises or timers");