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
  
701
  
702
  
703
  
704
  
705
  
706
  
707
  
708
  
709
  
710
  
711
  
712
  
713
  
714
  
715
  
716
  
717
  
718
  
719
  
720
  
721
  
722
  
723
  
724
  
725
  
726
  
727
  
728
  
729
  
730
  
731
  
732
  
733
  
734
  
735
  
736
  
737
  
738
  
739
  
740
  
741
  
742
  
743
  
744
  
745
  
746
  
747
  
748
  
749
  
750
  
751
  
752
  
753
  
754
  
755
  
756
  
757
  
758
  
759
  
760
  
761
  
762
  
763
  
764
  
765
  
766
  
767
  
768
  
769
  
770
  
771
  
772
  
773
  
774
  
775
  
776
  
777
  
778
  
779
  
780
  
781
  
#pike __REAL_VERSION__ 
 
//! Module for handling multiple concurrent events. 
//! 
//! The @[Future] and @[Promise] API was inspired by 
//! @url{https://github.com/couchdeveloper/FutureLib@}. 
 
protected enum State { 
  STATE_PENDING = 0, 
  STATE_FULFILLED, 
  STATE_REJECTED, 
}; 
 
protected Thread.Mutex mux = Thread.Mutex(); 
protected Thread.Condition cond = Thread.Condition(); 
 
//! Global failure callback, called when a promise without failure 
//! callback fails. This is useful to log exceptions, so they are not 
//! just silently caught and ignored. 
void on_failure(function(mixed : void) f) 
{ 
  global_on_failure = f; 
} 
protected function(mixed : void) global_on_failure; 
 
//! Value that will be provided asynchronously 
//! sometime in the future. 
//! 
//! @seealso 
//!   @[Promise] 
class Future 
{ 
  mixed result = UNDEFINED; 
  State state; 
 
  protected array(array(function(mixed, mixed ...: void)|array(mixed))) 
    success_cbs = ({}); 
  protected array(array(function(mixed, mixed ...: void)|array(mixed))) 
    failure_cbs = ({}); 
 
  //! Wait for fulfillment and return the value. 
  //! 
  //! @throws 
  //!   Throws on rejection. 
  mixed get() 
  { 
    State s = state; 
    mixed res = result; 
    if (!s) { 
      object key = mux->lock(); 
      while (!state) { 
        cond->wait(key); 
      } 
 
      s = state; 
      res = result; 
      key = 0; 
    } 
 
    if (s == STATE_REJECTED) { 
      throw(res); 
    } 
    return res; 
  } 
 
  //! Register a callback that is to be called on fulfillment. 
  //! 
  //! @param cb 
  //!   Function to be called. The first argument will be the 
  //!   result of the @[Future]. 
  //! 
  //! @param extra 
  //!   Any extra context needed for @[cb]. They will be provided 
  //!   as arguments two and onwards when @[cb] is called. 
  //! 
  //! @note 
  //!   @[cb] will always be called from the main backend. 
  //! 
  //! @seealso 
  //!   @[on_failure()] 
  this_program on_success(function(mixed, mixed ... : void) cb, mixed ... extra) 
  { 
    object key = mux->lock(); 
 
    if (state == STATE_FULFILLED) { 
      call_out(cb, 0, result, @extra); 
      key = 0; 
      return this; 
    } 
 
    success_cbs += ({ ({ cb, extra }) }); 
    key = 0; 
 
    return this; 
  } 
 
  //! Register a callback that is to be called on failure. 
  //! 
  //! @param cb 
  //!   Function to be called. The first argument will be the 
  //!   failure result of the @[Future]. 
  //! 
  //! @param extra 
  //!   Any extra context needed for @[cb]. They will be provided 
  //!   as arguments two and onwards when @[cb] is called. 
  //! 
  //! @note 
  //!   @[cb] will always be called from the main backend. 
  //! 
  //! @seealso 
  //!   @[on_success()] 
  this_program on_failure(function(mixed, mixed ... : void) cb, mixed ... extra) 
  { 
    object key = mux->lock(); 
 
    if (state == STATE_REJECTED) { 
      call_out(cb, 0, result, @extra); 
      key = 0; 
      return this; 
    } 
 
    failure_cbs += ({ ({ cb, extra }) }); 
    key = 0; 
 
    return this; 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with the result. 
  protected void apply(mixed val, Promise p, 
                       function(mixed, mixed ... : mixed) fun, 
                       array(mixed) ctx) 
  { 
    mixed err = catch { 
        p->success(fun(val, @ctx)); 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with the eventual result. 
  protected void apply_flat(mixed val, Promise p, 
                            function(mixed, mixed ... : Future) fun, 
                            array(mixed) ctx) 
  { 
    mixed err = catch { 
        Future f = fun(val, @ctx); 
        if (!objectp(f) || !f->on_failure || !f->on_success) { 
          error("Expected %O to return a Future. Got: %O.\n", 
                fun, f); 
        } 
        f->on_failure(p->failure); 
        f->on_success(p->success); 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with the eventual result. 
  protected void apply_smart(mixed val, Promise p, 
                            function(mixed, mixed ... : mixed|Future) fun, 
                            array(mixed) ctx) 
  { 
    mixed err = catch { 
        mixed|Future f = fun(val, @ctx); 
        if (!objectp(f) 
         || !functionp(f->on_failure) || !functionp(f->on_success)) { 
          p->success(f); 
          return; 
        } 
        f->on_failure(p->failure); 
        f->on_success(p->success); 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Apply @[fun] with @[val] followed by the contents of @[ctx], 
  //! and update @[p] with @[val] if @[fun] didn't return false. 
  //! If @[fun] returned false fail @[p] with @[UNDEFINED]. 
  protected void apply_filter(mixed val, Promise p, 
                              function(mixed, mixed ... : int(0..1)) fun, 
                              array(mixed) ctx) 
  { 
    mixed err = catch { 
        if (fun(val, @ctx)) { 
          p->success(val); 
        } else { 
          p->failure(UNDEFINED); 
        } 
        return; 
      }; 
    p->failure(err); 
  } 
 
  //! Return a @[Future] that will be fulfilled with the result 
  //! of applying @[fun] with the fulfilled result of this @[Future] 
  //! followed by @[extra]. 
  this_program map(function(mixed, mixed ... : mixed) fun, mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with the fulfilled result 
  //! of applying @[fun] with the fulfilled result of this @[Future] 
  //! followed by @[extra]. 
  this_program flat_map(function(mixed, mixed ... : this_program) fun, 
                        mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply_flat, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of this @[Future], or the result 
  //! of applying @[fun] with the failed result followed 
  //! by @[extra]. 
  this_program recover(function(mixed, mixed ... : mixed) fun, 
                       mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(p->success); 
    on_failure(apply, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of this @[Future], or the fulfilled result 
  //! of applying @[fun] with the failed result followed 
  //! by @[extra]. 
  this_program recover_with(function(mixed, mixed ... : this_program) fun, 
                            mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(p->success); 
    on_failure(apply_flat, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that either will by fulfilled by the 
  //! fulfilled result of this @[Future] if applying @[fun] 
  //! with the result followed by @[extra] returns true, 
  //! or will fail with @[UNDEFINED] if it returns false. 
  this_program filter(function(mixed, mixed ... : int(0..1)) fun, 
                      mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(apply_filter, p, fun, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the result of applying @[success] with the fulfilled result 
  //! followed by @[extra], or the result of applying @[failure] 
  //! with the failed result followed by @[extra]. 
  //! 
  //! @[failure] defaults to @[success]. 
  //! 
  //! @seealso 
  //!   @[map] 
  this_program transform(function(mixed, mixed ... : mixed) success, 
                         function(mixed, mixed ... : mixed)|void failure, 
                         mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(apply, p, success, extra); 
    on_failure(apply, p, failure || success, extra); 
    return p->future(); 
  } 
 
  //! Return a @[Future] that will be fulfilled with either 
  //! the fulfilled result of applying @[success] with the fulfilled result 
  //! followed by @[extra], or the fulfilled result of applying @[failure] 
  //! with the failed result followed by @[extra]. 
  //! 
  //! @[failure] defaults to @[success]. 
  //! 
  //! @seealso 
  //!   @[flat_map] 
  this_program transform_with(function(mixed, mixed ... : Future) success, 
                              function(mixed, mixed ... : Future)|void failure, 
                              mixed ... extra) 
  { 
    Promise p = Promise(); 
    on_success(apply_flat, p, success, extra); 
    on_failure(apply_flat, p, failure || success, extra); 
    return p->future(); 
  } 
 
  //! @returns 
  //! A @[Future] that will be fulfilled with an 
  //! array of the fulfilled result of this object followed 
  //! by the fulfilled results of @[others]. 
  //! 
  //! @seealso 
  //!   @[results()] 
  this_program zip(this_program ... others) 
  { 
    if (!sizeof(others)) return this_program::this; 
    return results(({ this_program::this }) + others); 
  } 
 
  //! JavaScript Promise API close but not identical equivalent 
  //! of a combined @[transform()] and @[transform_with()]. 
  //! 
  //! @param onfulfilled 
  //!   Function to be called on fulfillment. The first argument will be the 
  //!   result of @b{this@} @[Future]. 
  //!   The return value will be the result of the new @[Future]. 
  //!   If the return value already is a @[Future], pass it as-is. 
  //! 
  //! @param onrejected 
  //!   Function to be called on failure. The first argument will be the 
  //!   failure result of @b{this@} @[Future]. 
  //!   The return value will be the failure result of the new @[Future]. 
  //!   If the return value already is a @[Future], pass it as-is. 
  //! 
  //! @param extra 
  //!   Any extra context needed for @expr{onfulfilled@} and 
  //!   @expr{onrejected@}. They will be provided 
  //!   as arguments two and onwards when the callbacks are called. 
  //! 
  //! @returns 
  //! The new @[Future]. 
  //! 
  //! @seealso 
  //!   @[transform()], @[transform_with()], @[thencatch()], 
  //!   @[on_success()], @[Promise.success()], 
  //!   @[on_failure()], @[Promise.failure()], 
  //!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
  this_program then(void|function(mixed, mixed ... : mixed) onfulfilled, 
   void|function(mixed, mixed ... : mixed) onrejected, 
   mixed ... extra) { 
    Promise p = Promise(); 
    if (onfulfilled) 
      on_success(apply_smart, p, onfulfilled, extra); 
    else 
      on_success(p->success); 
    if (onrejected) 
      on_failure(apply_smart, p, onrejected, extra); 
    else 
      on_failure(p->failure); 
    return p->future(); 
  } 
 
  //! JavaScript Promise API equivalent of a combination of @[recover()] 
  //! and @[recover_with()]. 
  //! 
  //! @param onrejected 
  //!   Function to be called. The first argument will be the 
  //!   failure result of @b{this@} @[Future]. 
  //!   The return value will the failure result of the new @[Future]. 
  //!   If the return value already is a @[Future], pass it as-is. 
  //! 
  //! @param extra 
  //!   Any extra context needed for 
  //!   @expr{onrejected@}. They will be provided 
  //!   as arguments two and onwards when the callback is called. 
  //! 
  //! @returns 
  //! The new @[Future]. 
  //! 
  //! @seealso 
  //!   @[recover()], @[recover_with()], @[then()], @[on_failure()], 
  //!   @[Promise.failure()], 
  //!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
  inline this_program thencatch(function(mixed, mixed ... : mixed) onrejected, 
   mixed ... extra) { 
    return then(0, onrejected, @extra); 
  } 
 
  //! Return a @[Future] that will either be fulfilled with the fulfilled 
  //! result of this @[Future], or be failed after @[seconds] have expired. 
  this_program timeout(int|float seconds) 
  { 
    Promise p = Promise(); 
    on_failure(p->failure); 
    on_success(p->success); 
    call_out(p->try_failure, seconds, ({ "Timeout.\n", backtrace() })); 
    return p->future(); 
  } 
 
  protected string _sprintf(int t) 
  { 
    return t=='O' && sprintf("%O(%s,%O)", this_program, 
                             ([ STATE_PENDING : "pending", 
                                STATE_REJECTED : "rejected", 
                                STATE_FULFILLED : "fulfilled" ])[state], 
                             result); 
  } 
} 
 
//! Promise to provide a @[Future] value. 
//! 
//! Objects of this class are typically kept internal to the 
//! code that provides the @[Future] value. The only thing 
//! that is directly returned to the user is the return 
//! value from @[future()]. 
//! 
//! @seealso 
//!   @[Future], @[future()] 
class Promise 
{ 
  inherit Future; 
 
  //! Creates a new promise, optionally initialised from a traditional callback 
  //! driven method via @expr{executor(resolve, reject, extra ... )@}. 
  //! 
  //! @seealso 
  //!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
  protected void create(void| 
   function(function(mixed:void), 
            function(mixed:void), mixed ...:void) executor, mixed ... extra) { 
    if (executor) 
      executor(success, failure, @extra); 
  } 
 
  //! The future value that we promise. 
  Future future() 
  { 
    return Future::this; 
  } 
 
  protected void unlocked_success(mixed value) 
  { 
    if (state < STATE_REJECTED) { 
      result = value; 
      state = STATE_FULFILLED; 
      cond->broadcast(); 
      foreach(success_cbs, 
              [function(mixed, mixed ...: void) cb, 
               array(mixed) extra]) { 
        if (cb) { 
          call_out(cb, 0, value, @extra); 
        } 
      } 
    } 
  } 
 
  //! Fulfill the @[Future]. 
  //! 
  //! @param value 
  //!   Result of the @[Future]. 
  //! 
  //! @throws 
  //!   Throws an error if the @[Future] already has been fulfilled 
  //!   or failed. 
  //! 
  //! Mark the @[Future] as fulfilled, and schedule the @[on_success()] 
  //! callbacks to be called as soon as possible. 
  //! 
  //! @seealso 
  //!   @[try_success()], @[try_failure()], @[failure()], @[on_success()] 
  void success(mixed value) 
  { 
    if (state) error("Promise has already been finalized.\n"); 
    object key = mux->lock(); 
    if (state) error("Promise has already been finalized.\n"); 
    unlocked_success(value); 
    key = 0; 
  } 
 
  //! Fulfill the @[Future] if it hasn't been fulfilled or failed already. 
  //! 
  //! @param value 
  //!   Result of the @[Future]. 
  //! 
  //! Mark the @[Future] as fulfilled if it hasn't already been fulfilled 
  //! or failed, and in that case schedule the @[on_success()] callbacks 
  //! to be called as soon as possible. 
  //! 
  //! @seealso 
  //!   @[success()], @[try_failure()], @[failure()], @[on_success()] 
  void try_success(mixed value) 
  { 
    if (state) return; 
    object key = mux->lock(); 
    if (state) return; 
    unlocked_success(value); 
    key = 0; 
  } 
 
  protected void unlocked_failure(mixed value) 
  { 
    state = STATE_REJECTED; 
    result = value; 
    cond->broadcast(); 
    if( !sizeof(failure_cbs) && global_on_failure ) 
      call_out(global_on_failure, 0, value); 
    foreach(failure_cbs, 
            [function(mixed, mixed ...: void) cb, 
             array(mixed) extra]) { 
      if (cb) { 
        call_out(cb, 0, value, @extra); 
      } 
    } 
  } 
 
  //! Reject the @[Future] value. 
  //! 
  //! @param value 
  //!   Failure result of the @[Future]. 
  //! 
  //! @throws 
  //!   Throws an error if the @[Future] already has been fulfilled 
  //!   or failed. 
  //! 
  //! Mark the @[Future] as failed, and schedule the @[on_failure()] 
  //! callbacks to be called as soon as possible. 
  //! 
  //! @seealso 
  //!   @[try_failure()], @[success()], @[on_failure()] 
  void failure(mixed value) 
  { 
    if (state) error("Promise has already been finalized.\n"); 
    object key = mux->lock(); 
    if (state) error("Promise has already been finalized.\n"); 
    unlocked_failure(value); 
    key = 0; 
  } 
 
  //! Maybe reject the @[Future] value. 
  //! 
  //! @param value 
  //!   Failure result of the @[Future]. 
  //! 
  //! Mark the @[Future] as failed if it hasn't already been fulfilled, 
  //! and in that case schedule the @[on_failure()] callbacks to be 
  //! called as soon as possible. 
  //! 
  //! @seealso 
  //!   @[failure()], @[success()], @[on_failure()] 
  void try_failure(mixed value) 
  { 
    if (state) return; 
    object key = mux->lock(); 
    if (state) return; 
    unlocked_failure(value); 
  } 
 
  protected void destroy() 
  { 
    if (!state) { 
      unlocked_failure(({ "Promise broken.\n", backtrace() })); 
    } 
  } 
} 
 
protected class FirstCompleted 
{ 
  inherit Promise; 
 
  protected void create(array(Future) futures) 
  { 
    if (!sizeof(futures)) { 
      state = STATE_FULFILLED; 
      return; 
    } 
    futures->on_failure(try_failure); 
    futures->on_success(try_success); 
  } 
} 
 
//! @returns 
//! A @[Future] that represents the first 
//! of the @expr{futures@} that completes. 
//! 
//! @seealso 
//!   @[race()] 
variant Future first_completed(array(Future) futures) 
{ 
  Promise p = FirstCompleted(futures); 
  return p->future(); 
} 
variant inline Future first_completed(Future ... futures) 
{ 
  return first_completed(futures); 
} 
 
//! JavaScript Promise API equivalent of @[first_completed()]. 
//! 
//! @seealso 
//!   @[first_completed()] 
//!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
variant inline Future race(array(Future) futures) 
{ 
  return first_completed(futures); 
} 
variant inline Future race(Future ... futures) 
{ 
  return first_completed(futures); 
} 
 
protected class Results 
{ 
  inherit Promise; 
 
  protected void create(array(Future) futures) 
  { 
    if (!sizeof(futures)) { 
      success(({})); 
      return; 
    } 
 
    array(mixed) results = allocate(sizeof(futures), UNDEFINED); 
    array(State) states  = allocate(sizeof(futures), STATE_PENDING); 
 
    futures->on_failure(failure); 
 
    foreach(futures; int i; Future f) { 
      f->on_success(got_success, i, results, states); 
    } 
  } 
 
  protected void got_success(mixed value, int i, 
                             array(mixed) results, array(State) states) 
  { 
    if (state || states[i]) return; 
    object key = mux->lock(); 
    if (state || states[i]) return; 
    results[i] = value; 
    states[i] = STATE_FULFILLED; 
    if (has_value(states, STATE_PENDING)) { 
      return; 
    } 
    key = 0; 
    success(results); 
  } 
} 
 
//! @returns 
//! A @[Future] that represents the array of all the completed @expr{futures@}. 
//! 
//! @seealso 
//!   @[all()] 
variant Future results(array(Future) futures) 
{ 
  Promise p = Results(futures); 
  return p->future(); 
} 
inline variant Future results(Future ... futures) 
{ 
  return results(futures); 
} 
 
//! JavaScript Promise API equivalent of @[results()]. 
//! 
//! @seealso 
//!   @[results()] 
//!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
inline variant Future all(array(Future) futures) 
{ 
  return results(futures); 
} 
inline variant Future all(Future ... futures) 
{ 
  return results(futures); 
} 
 
//! @returns 
//! A new @[Future] that has already failed for the specified @expr{reason@}. 
//! 
//! @seealso 
//!   @[Future.on_failure()], @[Promise.failure()] 
//!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
Future reject(mixed reason) 
{ 
  object p = Promise(); 
  p->failure(reason); 
  return p->future(); 
} 
 
//! @returns 
//! A new @[Future] that has already been fulfilled with @expr{value@} 
//! as result.  If @expr{value@} is an object which already 
//! has @[on_failure] and @[on_success] methods, return it unchanged. 
//! 
//! @note 
//! This function can be used to ensure values are futures. 
//! 
//! @seealso 
//!   @[Future.on_success()], @[Promise.success()] 
//!   @url{https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise@} 
Future resolve(mixed value) 
{ 
  if (objectp(value) && value->on_failure && value->on_success) 
    return value; 
  object p = Promise(); 
  p->success(value); 
  return p->future(); 
} 
 
//! Return a @[Future] that represents the array of mapping @[fun] 
//! over the results of the completed @[futures]. 
Future traverse(array(Future) futures, 
                function(mixed, mixed ... : mixed) fun, 
                mixed ... extra) 
{ 
  return results(futures->map(fun, @extra)); 
} 
 
protected class Fold 
{ 
  inherit Promise; 
 
  protected mixed accumulated; 
 
  protected void create(array(Future) futures, 
                        mixed initial, 
                        function(mixed, mixed, mixed ... : mixed) fun, 
                        array(mixed) ctx) 
  { 
    if (!sizeof(futures)) { 
      success(initial); 
      return; 
    } 
    accumulated = initial; 
    futures->on_failure(failure); 
    foreach(futures; int i; Future f) { 
      f->on_success(got_success, i, fun, ctx, 
                    allocate(sizeof(futures), STATE_PENDING)); 
    } 
  } 
 
  protected void got_success(mixed val, int i, 
                             function(mixed, mixed, mixed ... : mixed) fun, 
                             array(mixed) ctx, 
                             array(State) states) 
  { 
    if (state || states[i]) return; 
    object key = mux->lock(); 
    if (state || states[i]) return; 
    states[i] = STATE_FULFILLED; 
    mixed err = catch { 
        // FIXME: What if fun triggers a recursive call? 
        accumulated = fun(val, accumulated, @ctx); 
        if (has_value(states, STATE_PENDING)) return; 
        success(accumulated); 
        return; 
      }; 
    failure(err); 
  } 
} 
 
//! Return a @[Future] that represents the accumulated results of 
//! applying @[fun] to the results of the @[futures] in turn. 
//! 
//! @param initial 
//!   Initial value of the accumulator. 
//! 
//! @param fun 
//!   Function to apply. The first argument is the result of 
//!   one of the @[futures], the second the current accumulated 
//!   value, and any further from @[extra]. 
//! 
//! @note 
//!   If @[fun] throws an error it will fail the @[Future]. 
//! 
//! @note 
//!   @[fun] may be called in any order, and will be called 
//!   once for every @[Future] in @[futures], unless one of 
//!   calls fails in which case no further calls will be 
//!   performed. 
Future fold(array(Future) futures, 
            mixed initial, 
            function(mixed, mixed, mixed ... : mixed) fun, 
            mixed ... extra) 
{ 
  Promise p = Fold(futures, initial, fun, extra); 
  return p->future(); 
}