promises.js 25.3 KB
Newer Older
rossberg@chromium.org's avatar
rossberg@chromium.org committed
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
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax
rossberg@chromium.org's avatar
rossberg@chromium.org committed
29

30 31
// Make sure we don't rely on functions patchable by monkeys.
var call = Function.prototype.call.call.bind(Function.prototype.call)
32 33
var getOwnPropertyNames = Object.getOwnPropertyNames;
var defineProperty = Object.defineProperty;
34 35
var numberPrototype = Number.prototype;
var symbolIterator = Symbol.iterator;
36

37 38 39
function assertUnreachable() {
  %AbortJS("Failure: unreachable");
}
40 41 42 43 44 45 46

(function() {
  // Test before clearing global (fails otherwise)
  assertEquals("[object Promise]",
      Object.prototype.toString.call(new Promise(function() {})));
})();

47 48 49 50 51 52

function clear(o) {
  if (o === null || (typeof o !== 'object' && typeof o !== 'function')) return
  clear(o.__proto__)
  var properties = getOwnPropertyNames(o)
  for (var i in properties) {
53 54
    // Do not clobber Object.prototype.toString, which is used by tests.
    if (properties[i] === "toString") continue;
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    clearProp(o, properties[i])
  }
}

function clearProp(o, name) {
  var poisoned = {caller: 0, callee: 0, arguments: 0}
  try {
    var x = o[name]
    o[name] = undefined
    clear(x)
  } catch(e) {} // assertTrue(name in poisoned) }
}

// Find intrinsics and null them out.
var globals = Object.getOwnPropertyNames(this)
70 71 72 73 74 75 76 77 78
var whitelist = {
  Promise: true,
  TypeError: true,
  String: true,
  JSON: true,
  Error: true,
  MjsUnitAssertionError: true
};

79 80 81 82 83 84 85 86 87
for (var i in globals) {
  var name = globals[i]
  if (name in whitelist || name[0] === name[0].toLowerCase()) delete globals[i]
}
for (var i in globals) {
  if (globals[i]) clearProp(this, globals[i])
}


88 89 90 91 92 93
function defer(constructor) {
  var resolve, reject;
  var promise = new constructor((res, rej) => { resolve = res; reject = rej });
  return { promise, resolve, reject };
}

rossberg@chromium.org's avatar
rossberg@chromium.org committed
94 95 96 97 98 99 100 101 102 103 104 105 106
var asyncAssertsExpected = 0;

function assertAsyncRan() { ++asyncAssertsExpected }

function assertAsync(b, s) {
  if (b) {
    print(s, "succeeded")
  } else {
    %AbortJS(s + " FAILED!")  // Simply throwing here will have no effect.
  }
  --asyncAssertsExpected
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
function assertLater(f, name) {
  assertFalse(f()); // should not be true synchronously
  ++asyncAssertsExpected;
  var iterations = 0;
  function runAssertion() {
    if (f()) {
      print(name, "succeeded");
      --asyncAssertsExpected;
    } else if (iterations++ < 10) {
      %EnqueueMicrotask(runAssertion);
    } else {
      %AbortJS(name + " FAILED!");
    }
  }
  %EnqueueMicrotask(runAssertion);
}

rossberg@chromium.org's avatar
rossberg@chromium.org committed
124
function assertAsyncDone(iteration) {
125 126 127 128 129
  var iteration = iteration || 0;
  %EnqueueMicrotask(function() {
    if (asyncAssertsExpected === 0)
      assertAsync(true, "all")
    else if (iteration > 10)  // Shouldn't take more.
130
      assertAsync(false, "all... " + asyncAssertsExpected)
131 132 133
    else
      assertAsyncDone(iteration + 1)
  });
rossberg@chromium.org's avatar
rossberg@chromium.org committed
134 135
}

136 137 138 139 140 141 142 143
(function() {
  assertThrows(function() { Promise(function() {}) }, TypeError)
})();

(function() {
  assertTrue(new Promise(function() {}) instanceof Promise)
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
144 145 146 147 148
(function() {
  assertThrows(function() { new Promise(5) }, TypeError)
})();

(function() {
149 150 151 152
  assertDoesNotThrow(function() { new Promise(function() { throw 5 }) })
})();

(function() {
153
  (new Promise(function() { throw 5 })).then(
154 155 156 157
    assertUnreachable,
    function(r) { assertAsync(r === 5, "new-throw") }
  )
  assertAsyncRan()
rossberg@chromium.org's avatar
rossberg@chromium.org committed
158 159 160
})();

(function() {
161 162 163
  Promise.resolve(5);
  Promise.resolve(5).then(undefined, assertUnreachable).then(
    function(x) { assertAsync(x === 5, "resolved/then-nohandler") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
164 165 166 167 168 169
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
170
  Promise.resolve(5).then(undefined, assertUnreachable).then(
171 172 173 174
    function(x) { assertAsync(x === 5, "resolved/then-nohandler-undefined") },
    assertUnreachable
  )
  assertAsyncRan()
175
  Promise.resolve(6).then(null, assertUnreachable).then(
176
    function(x) { assertAsync(x === 6, "resolved/then-nohandler-null") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
177 178 179 180 181 182
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
183 184 185
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
186 187 188 189 190 191 192 193
  p3.then(
    function(x) { assertAsync(x === 5, "resolved/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
194
  var p1 = Promise.reject(5)
195 196
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
197 198 199 200 201 202 203 204
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "rejected/then") }
  )
  assertAsyncRan()
})();

(function() {
205 206 207 208 209
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(function(x) { return x }, assertUnreachable).then(
    function(x) { assertAsync(x === 5, "resolved/then/then") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
210 211 212 213 214 215
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
216 217 218 219
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(function(x){ return Promise.resolve(x+1) }, assertUnreachable).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
220 221 222 223 224 225 226
    function(x) { assertAsync(x === 6, "resolved/then/then2") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
227 228 229 230
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(function(x) { throw 6 }, assertUnreachable).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
231
    assertUnreachable,
232
    function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
233 234 235 236 237
  )
  assertAsyncRan()
})();

(function() {
238 239 240 241
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(function(x) { throw 6 }, assertUnreachable).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
242
    assertUnreachable,
243
    function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
244 245 246 247 248
  )
  assertAsyncRan()
})();

(function() {
249 250 251 252
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(function(x) { throw 6 }, assertUnreachable).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
253
    assertUnreachable,
254
    function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
255 256 257 258 259
  )
  assertAsyncRan()
})();

(function() {
260 261 262
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
263 264 265 266 267 268 269 270
  p3.then(function(x) { throw 6 }, assertUnreachable).then(
    assertUnreachable,
    function(x) { assertAsync(x === 6, "resolved/then-throw/then") }
  )
  assertAsyncRan()
})();

(function() {
271
  var p1 = Promise.resolve(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
272
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
273 274 275
  var p3 = Promise.resolve(p2)
  p3.then(
    function(x) { assertAsync(x === 5, "resolved/thenable/then") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
276 277 278 279 280 281
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
282
  var p1 = Promise.resolve(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
283
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
284
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
285 286 287 288 289 290 291 292
  p3.then(
    function(x) { assertAsync(x === 5, "resolved/thenable/then") },
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
293
  var p1 = Promise.reject(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
294
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
295 296
  var p3 = Promise.resolve(p2)
  p3.then(
297
    assertUnreachable,
298
    function(x) { assertAsync(x === 5, "rejected/thenable/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
299 300 301 302 303
  )
  assertAsyncRan()
})();

(function() {
304
  var p1 = Promise.reject(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
305
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
306
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
307 308 309 310 311 312 313 314
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "rejected/thenable/then") }
  )
  assertAsyncRan()
})();

(function() {
315
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
316
  var p1 = deferred.promise
317 318 319 320
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
321 322 323 324 325 326 327
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
328
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
329
  var p1 = deferred.promise
330 331
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
332 333 334 335 336 337 338 339 340
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
341
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
342
  var p1 = deferred.promise
343 344 345
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
  p3.then(
346
    assertUnreachable,
347
    function(x) { assertAsync(x === 5, "then/reject") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
348 349 350 351 352 353
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
354
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
355
  var p1 = deferred.promise
356 357
  var p2 = Promise.resolve(p1)
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
358 359 360 361 362 363 364 365
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

366
(function() {
367
  var deferred = defer(Promise)
368 369 370 371 372 373 374 375 376 377 378
  var p1 = deferred.promise
  var p2 = p1.then(1, 2)
  p2.then(
    function(x) { assertAsync(x === 5, "then/resolve-non-function") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
379
  var deferred = defer(Promise)
380 381 382 383 384 385 386 387 388 389
  var p1 = deferred.promise
  var p2 = p1.then(1, 2)
  p2.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject-non-function") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
390
(function() {
391
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
392 393
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
394 395 396
  var p3 = Promise.resolve(p2)
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
397 398 399 400 401 402 403
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
404
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
405 406
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
407
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
408 409 410 411 412 413 414 415 416
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable") },
    assertUnreachable
  )
  deferred.resolve(5)
  assertAsyncRan()
})();

(function() {
417
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
418 419
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
420 421
  var p3 = Promise.resolve(p2)
  p3.then(
422
    assertUnreachable,
423
    function(x) { assertAsync(x === 5, "then/reject/thenable") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
424 425 426 427 428 429
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
430
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
431 432
  var p1 = deferred.promise
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
433
  var p3 = Promise.resolve(p2)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
434 435 436 437 438 439 440 441 442
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject/thenable") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
443 444 445
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
446
  var p3 = deferred.promise
447 448
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve2") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
449 450 451 452
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
453
})();
rossberg@chromium.org's avatar
rossberg@chromium.org committed
454 455

(function() {
456 457 458
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
459 460 461 462 463 464 465 466 467 468
  var p3 = deferred.promise
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
})();

(function() {
469 470 471
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
472
  var p3 = deferred.promise
473
  p3.then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
474
    assertUnreachable,
475
    function(x) { assertAsync(x === 5, "then/reject2") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
476 477 478 479 480 481
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
482 483 484
  var p1 = Promise.resolve(5)
  var p2 = Promise.resolve(p1)
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
485 486 487 488 489 490 491 492 493 494
  var p3 = deferred.promise
  p3.then(
    assertUnreachable,
    function(x) { assertAsync(x === 5, "then/reject2") }
  )
  deferred.reject(5)
  assertAsyncRan()
})();

(function() {
495
  var p1 = Promise.resolve(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
496
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
497
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
498
  var p3 = deferred.promise
499 500
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
501 502 503 504
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
505
})();
rossberg@chromium.org's avatar
rossberg@chromium.org committed
506 507

(function() {
508
  var p1 = Promise.resolve(5)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
509
  var p2 = {then: function(onResolve, onReject) { onResolve(p1) }}
510
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
511 512 513 514 515 516 517 518 519 520
  var p3 = deferred.promise
  p3.then(
    function(x) { assertAsync(x === 5, "then/resolve/thenable2") },
    assertUnreachable
  )
  deferred.resolve(p2)
  assertAsyncRan()
})();

(function() {
521 522 523
  var p1 = Promise.resolve(0)
  var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
  p2.then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
524
    assertUnreachable,
525
    function(r) { assertAsync(r instanceof TypeError, "cyclic/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
526 527 528 529 530
  )
  assertAsyncRan()
})();

(function() {
531
  var p1 = Promise.resolve(0)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
532
  var p2 = p1.then(function(x) { return p2 }, assertUnreachable)
533
  p2.then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
534 535 536 537 538 539 540
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/then") }
  )
  assertAsyncRan()
})();

(function() {
541
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
542 543
  var p = deferred.promise
  deferred.resolve(p)
544
  p.then(
545 546
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") }
rossberg@chromium.org's avatar
rossberg@chromium.org committed
547 548
  )
  assertAsyncRan()
549
})();
rossberg@chromium.org's avatar
rossberg@chromium.org committed
550 551

(function() {
552
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
553 554 555 556 557 558 559
  var p = deferred.promise
  deferred.resolve(p)
  p.then(
    assertUnreachable,
    function(r) { assertAsync(r instanceof TypeError, "cyclic/deferred/then") }
  )
  assertAsyncRan()
560
})();
rossberg@chromium.org's avatar
rossberg@chromium.org committed
561 562

(function() {
563
  Promise.all([]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
564 565 566 567 568 569
    function(x) { assertAsync(x.length === 0, "all/resolve/empty") },
    assertUnreachable
  )
  assertAsyncRan()
})();

570 571
(function() {
  function testPromiseAllNonIterable(value) {
572
    Promise.all(value).then(
573 574 575 576 577 578 579 580 581 582 583 584
        assertUnreachable,
        function(r) {
          assertAsync(r instanceof TypeError, 'all/non iterable');
        });
    assertAsyncRan();
  }
  testPromiseAllNonIterable(null);
  testPromiseAllNonIterable(undefined);
  testPromiseAllNonIterable({});
  testPromiseAllNonIterable(42);
})();

585 586 587 588 589 590 591 592 593
(function() {
  Promise.all({[symbolIterator](){ return null; }}).then(
    assertUnreachable,
    function(r) {
      assertAsync(r instanceof TypeError, 'all/non iterable');
    });
  assertAsyncRan();
})();

594
(function() {
595
  var deferred = defer(Promise);
596 597 598 599 600 601
  var p = deferred.promise;
  function* f() {
    yield 1;
    yield p;
    yield 3;
  }
602
  Promise.all(f()).then(
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
      function(x) {
        assertAsync(x.length === 3, "all/resolve/iterable");
        assertAsync(x[0] === 1, "all/resolve/iterable/0");
        assertAsync(x[1] === 2, "all/resolve/iterable/1");
        assertAsync(x[2] === 3, "all/resolve/iterable/2");
      },
      assertUnreachable);
  deferred.resolve(2);
  assertAsyncRan();
  assertAsyncRan();
  assertAsyncRan();
  assertAsyncRan();
})();


rossberg@chromium.org's avatar
rossberg@chromium.org committed
618
(function() {
619
  var deferred1 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
620
  var p1 = deferred1.promise
621
  var deferred2 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
622
  var p2 = deferred2.promise
623
  var deferred3 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
624
  var p3 = deferred3.promise
625
  Promise.all([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    function(x) {
      assertAsync(x.length === 3, "all/resolve")
      assertAsync(x[0] === 1, "all/resolve/0")
      assertAsync(x[1] === 2, "all/resolve/1")
      assertAsync(x[2] === 3, "all/resolve/2")
    },
    assertUnreachable
  )
  deferred1.resolve(1)
  deferred3.resolve(3)
  deferred2.resolve(2)
  assertAsyncRan()
  assertAsyncRan()
  assertAsyncRan()
  assertAsyncRan()
})();

(function() {
644
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
645
  var p1 = deferred.promise
646 647 648
  var p2 = Promise.resolve(2)
  var p3 = defer(Promise).promise
  Promise.all([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
649 650 651 652 653 654 655
    assertUnreachable,
    assertUnreachable
  )
  deferred.resolve(1)
})();

(function() {
656
  var deferred1 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
657
  var p1 = deferred1.promise
658
  var deferred2 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
659
  var p2 = deferred2.promise
660
  var deferred3 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
661
  var p3 = deferred3.promise
662
  Promise.all([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
663 664 665 666 667 668 669 670 671
    assertUnreachable,
    function(x) { assertAsync(x === 2, "all/reject") }
  )
  deferred1.resolve(1)
  deferred3.resolve(3)
  deferred2.reject(2)
  assertAsyncRan()
})();

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
(function() {
  'use strict';
  var getCalls = 0;
  var funcCalls = 0;
  var nextCalls = 0;
  defineProperty(numberPrototype, symbolIterator, {
    get: function() {
      assertEquals('number', typeof this);
      getCalls++;
      return function() {
        assertEquals('number', typeof this);
        funcCalls++;
        var n = this;
        var i = 0
        return {
          next() {
            nextCalls++;
            return {value: i++, done: i > n};
          }
        };
      };
    },
    configurable: true
  });

697
  Promise.all(3).then(
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      function(x) {
        assertAsync(x.length === 3, "all/iterable/number/length");
        assertAsync(x[0] === 0, "all/iterable/number/0");
        assertAsync(x[1] === 1, "all/iterable/number/1");
        assertAsync(x[2] === 2, "all/iterable/number/2");
      },
      assertUnreachable);
  delete numberPrototype[symbolIterator];

  assertEquals(getCalls, 1);
  assertEquals(funcCalls, 1);
  assertEquals(nextCalls, 3 + 1);  // + 1 for {done: true}
  assertAsyncRan();
  assertAsyncRan();
  assertAsyncRan();
  assertAsyncRan();
})();


rossberg@chromium.org's avatar
rossberg@chromium.org committed
717
(function() {
718
  Promise.race([]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
719 720 721 722 723 724
    assertUnreachable,
    assertUnreachable
  )
})();

(function() {
725 726 727 728
  var p1 = Promise.resolve(1)
  var p2 = Promise.resolve(2)
  var p3 = Promise.resolve(3)
  Promise.race([p1, p2, p3]).then(
729
    function(x) { assertAsync(x === 1, "resolved/one") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
730 731 732 733 734 735
    assertUnreachable
  )
  assertAsyncRan()
})();

(function() {
736 737 738 739
  var p1 = Promise.resolve(1)
  var p2 = Promise.resolve(2)
  var p3 = Promise.resolve(3)
  Promise.race([0, p1, p2, p3]).then(
740
    function(x) { assertAsync(x === 0, "resolved-const/one") },
rossberg@chromium.org's avatar
rossberg@chromium.org committed
741 742 743 744 745
    assertUnreachable
  )
  assertAsyncRan()
})();

746
(function() {
747
  var deferred1 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
748
  var p1 = deferred1.promise
749
  var deferred2 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
750
  var p2 = deferred2.promise
751
  var deferred3 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
752
  var p3 = deferred3.promise
753
  Promise.race([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
754 755 756 757 758 759 760 761 762
    function(x) { assertAsync(x === 3, "one/resolve") },
    assertUnreachable
  )
  deferred3.resolve(3)
  deferred1.resolve(1)
  assertAsyncRan()
})();

(function() {
763
  var deferred = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
764
  var p1 = deferred.promise
765 766 767
  var p2 = Promise.resolve(2)
  var p3 = defer(Promise).promise
  Promise.race([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
768 769 770 771 772 773 774 775
    function(x) { assertAsync(x === 2, "resolved/one") },
    assertUnreachable
  )
  deferred.resolve(1)
  assertAsyncRan()
})();

(function() {
776
  var deferred1 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
777
  var p1 = deferred1.promise
778
  var deferred2 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
779
  var p2 = deferred2.promise
780
  var deferred3 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
781
  var p3 = deferred3.promise
782
  Promise.race([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
783 784 785 786 787 788 789 790 791
    function(x) { assertAsync(x === 3, "one/resolve/reject") },
    assertUnreachable
  )
  deferred3.resolve(3)
  deferred1.reject(1)
  assertAsyncRan()
})();

(function() {
792
  var deferred1 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
793
  var p1 = deferred1.promise
794
  var deferred2 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
795
  var p2 = deferred2.promise
796
  var deferred3 = defer(Promise)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
797
  var p3 = deferred3.promise
798
  Promise.race([p1, p2, p3]).then(
rossberg@chromium.org's avatar
rossberg@chromium.org committed
799 800 801 802 803 804 805 806
    assertUnreachable,
    function(x) { assertAsync(x === 3, "one/reject/resolve") }
  )
  deferred3.reject(3)
  deferred1.resolve(1)
  assertAsyncRan()
})();

807 808 809

(function() {
  function testPromiseRaceNonIterable(value) {
810
    Promise.race(value).then(
811 812 813 814 815 816 817 818 819 820 821 822 823 824
        assertUnreachable,
        function(r) {
          assertAsync(r instanceof TypeError, 'race/non iterable');
        });
    assertAsyncRan();
  }
  testPromiseRaceNonIterable(null);
  testPromiseRaceNonIterable(undefined);
  testPromiseRaceNonIterable({});
  testPromiseRaceNonIterable(42);
})();


(function() {
825
  var deferred1 = defer(Promise)
826
  var p1 = deferred1.promise
827
  var deferred2 = defer(Promise)
828
  var p2 = deferred2.promise
829
  var deferred3 = defer(Promise)
830 831 832 833 834 835
  var p3 = deferred3.promise
  function* f() {
    yield p1;
    yield p2;
    yield p3;
  }
836
  Promise.race(f()).then(
837 838 839 840 841 842 843 844 845
    function(x) { assertAsync(x === 3, "race/iterable/resolve/reject") },
    assertUnreachable
  )
  deferred3.resolve(3)
  deferred1.reject(1)
  assertAsyncRan()
})();

(function() {
846
  var deferred1 = defer(Promise)
847
  var p1 = deferred1.promise
848
  var deferred2 = defer(Promise)
849
  var p2 = deferred2.promise
850
  var deferred3 = defer(Promise)
851 852 853 854 855 856
  var p3 = deferred3.promise
  function* f() {
    yield p1;
    yield p2;
    yield p3;
  }
857
  Promise.race(f()).then(
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
    assertUnreachable,
    function(x) { assertAsync(x === 3, "race/iterable/reject/resolve") }
  )
  deferred3.reject(3)
  deferred1.resolve(1)
  assertAsyncRan()
})();

(function() {
  'use strict';
  var getCalls = 0;
  var funcCalls = 0;
  var nextCalls = 0;
  defineProperty(numberPrototype, symbolIterator, {
    get: function() {
      assertEquals('number', typeof this);
      getCalls++;
      return function() {
        assertEquals('number', typeof this);
        funcCalls++;
        var n = this;
        var i = 0
        return {
          next() {
            nextCalls++;
            return {value: i++, done: i > n};
          }
        };
      };
    },
    configurable: true
  });

891
  Promise.race(3).then(
892 893 894 895 896 897 898 899 900 901 902 903
      function(x) {
        assertAsync(x === 0, "race/iterable/number");
      },
      assertUnreachable);
  delete numberPrototype[symbolIterator];

  assertEquals(getCalls, 1);
  assertEquals(funcCalls, 1);
  assertEquals(nextCalls, 3 + 1);  // + 1 for {done: true}
  assertAsyncRan();
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
904 905 906 907
(function() {
  var log
  function MyPromise(resolver) {
    log += "n"
908 909 910 911 912 913 914 915
    var promise = new Promise(function(resolve, reject) {
      resolver(
        function(x) { log += "x" + x; resolve(x) },
        function(r) { log += "r" + r; reject(r) }
      )
    })
    promise.__proto__ = MyPromise.prototype
    return promise
rossberg@chromium.org's avatar
rossberg@chromium.org committed
916 917 918
  }

  MyPromise.__proto__ = Promise
919
  MyPromise.defer = function() {
rossberg@chromium.org's avatar
rossberg@chromium.org committed
920
    log += "d"
921
    return call(this.__proto__.defer, this)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
922 923 924
  }

  MyPromise.prototype.__proto__ = Promise.prototype
925
  MyPromise.prototype.then = function(resolve, reject) {
926
    log += "c"
927
    return call(this.__proto__.__proto__.then, this, resolve, reject)
rossberg@chromium.org's avatar
rossberg@chromium.org committed
928 929 930 931 932
  }

  log = ""
  var p1 = new MyPromise(function(resolve, reject) { resolve(1) })
  var p2 = new MyPromise(function(resolve, reject) { reject(2) })
933
  var d3 = defer(MyPromise)
934 935
  assertTrue(d3.promise instanceof Promise, "subclass/instance")
  assertTrue(d3.promise instanceof MyPromise, "subclass/instance-my3")
936
  assertTrue(log === "nx1nr2n", "subclass/create")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
937 938

  log = ""
939 940
  var p4 = MyPromise.resolve(4)
  var p5 = MyPromise.reject(5)
941
  assertTrue(p4 instanceof MyPromise, "subclass/instance4")
942
  assertTrue(p4 instanceof MyPromise, "subclass/instance-my4")
943
  assertTrue(p5 instanceof MyPromise, "subclass/instance5")
944
  assertTrue(p5 instanceof MyPromise, "subclass/instance-my5")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
945 946 947 948
  d3.resolve(3)
  assertTrue(log === "nx4nr5x3", "subclass/resolve")

  log = ""
949 950
  var d6 = defer(MyPromise)
  d6.promise.then(function(x) {
951
    return new Promise(function(resolve) { resolve(x) })
952
  }).then(function() {})
rossberg@chromium.org's avatar
rossberg@chromium.org committed
953
  d6.resolve(6)
954
  assertTrue(log === "ncncnx6", "subclass/then")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
955 956

  log = ""
957
  Promise.all([11, Promise.resolve(12), 13, MyPromise.resolve(14), 15, 16])
958 959

  assertTrue(log === "nx14", "subclass/all/arg")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
960 961

  log = ""
962 963
  MyPromise.all([21, Promise.resolve(22), 23, MyPromise.resolve(24), 25, 26])
  assertTrue(log === "nx24nnx21cnnx[object Promise]cnnx23cncnnx25cnnx26cn",
964
             "subclass/all/self")
rossberg@chromium.org's avatar
rossberg@chromium.org committed
965 966
})();

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
(function() {
  'use strict';

  class Pact extends Promise { }
  class Vow  extends Pact    { }
  class Oath extends Vow     { }

  Oath.constructor = Vow;

  assertTrue(Pact.resolve(Pact.resolve()).constructor === Pact,
             "subclass/resolve/own");

  assertTrue(Pact.resolve(Promise.resolve()).constructor === Pact,
             "subclass/resolve/ancestor");

  assertTrue(Pact.resolve(Vow.resolve()).constructor === Pact,
             "subclass/resolve/descendant"); var vow = Vow.resolve();

  vow.constructor = Oath;
  assertTrue(Oath.resolve(vow) === vow,
             "subclass/resolve/descendant with transplanted own constructor");
}());
rossberg@chromium.org's avatar
rossberg@chromium.org committed
989

990 991 992 993 994 995 996
(function() {
  var thenCalled = false;

  var resolve;
  var promise = new Promise(function(res) { resolve = res; });
  resolve({ then() { thenCalled = true; throw new Error(); } });
  assertLater(function() { return thenCalled; }, "resolve-with-thenable");
997
})();
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024

(function() {
  var calledWith;

  var resolve;
  var p1 = (new Promise(function(res) { resolve = res; }));
  var p2 = p1.then(function(v) {
    return {
      then(resolve, reject) { resolve({ then() { calledWith = v }}); }
    };
  });

  resolve({ then(resolve) { resolve(2); } });
  assertLater(function() { return calledWith === 2; },
              "resolve-with-thenable2");
})();

(function() {
  var p = Promise.resolve();
  var callCount = 0;
  defineProperty(p, "constructor", {
    get: function() { ++callCount; return Promise; }
  });
  p.then();
  assertEquals(1, callCount);
})();

rossberg@chromium.org's avatar
rossberg@chromium.org committed
1025
assertAsyncDone()