11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst rejection = new Error('Swallowed reject');
71cb0ef41Sopenharmony_ciconst rejection2 = new TypeError('Weird');
81cb0ef41Sopenharmony_ciconst resolveMessage = 'First call';
91cb0ef41Sopenharmony_ciconst rejectPromise = new Promise((r) => setTimeout(r, 10, rejection2));
101cb0ef41Sopenharmony_ciconst swallowedResolve = 'Swallowed resolve';
111cb0ef41Sopenharmony_ciconst swallowedResolve2 = 'Foobar';
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciprocess.on('multipleResolves', common.mustCall(handler, 4));
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst p1 = new Promise((resolve, reject) => {
161cb0ef41Sopenharmony_ci  resolve(resolveMessage);
171cb0ef41Sopenharmony_ci  resolve(swallowedResolve);
181cb0ef41Sopenharmony_ci  reject(rejection);
191cb0ef41Sopenharmony_ci});
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst p2 = new Promise((resolve, reject) => {
221cb0ef41Sopenharmony_ci  reject(rejectPromise);
231cb0ef41Sopenharmony_ci  resolve(swallowedResolve2);
241cb0ef41Sopenharmony_ci  reject(rejection2);
251cb0ef41Sopenharmony_ci}).catch(common.mustCall((exception) => {
261cb0ef41Sopenharmony_ci  assert.strictEqual(exception, rejectPromise);
271cb0ef41Sopenharmony_ci}));
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ciconst expected = [
301cb0ef41Sopenharmony_ci  'resolve',
311cb0ef41Sopenharmony_ci  p1,
321cb0ef41Sopenharmony_ci  swallowedResolve,
331cb0ef41Sopenharmony_ci  'reject',
341cb0ef41Sopenharmony_ci  p1,
351cb0ef41Sopenharmony_ci  rejection,
361cb0ef41Sopenharmony_ci  'resolve',
371cb0ef41Sopenharmony_ci  p2,
381cb0ef41Sopenharmony_ci  swallowedResolve2,
391cb0ef41Sopenharmony_ci  'reject',
401cb0ef41Sopenharmony_ci  p2,
411cb0ef41Sopenharmony_ci  rejection2,
421cb0ef41Sopenharmony_ci];
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_cilet count = 0;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cifunction handler(type, promise, reason) {
471cb0ef41Sopenharmony_ci  assert.strictEqual(type, expected.shift());
481cb0ef41Sopenharmony_ci  // In the first two cases the promise is identical because it's not delayed.
491cb0ef41Sopenharmony_ci  // The other two cases are not identical, because the `promise` is caught in a
501cb0ef41Sopenharmony_ci  // state when it has no knowledge about the `.catch()` handler that is
511cb0ef41Sopenharmony_ci  // attached to it right afterwards.
521cb0ef41Sopenharmony_ci  if (count++ < 2) {
531cb0ef41Sopenharmony_ci    assert.strictEqual(promise, expected.shift());
541cb0ef41Sopenharmony_ci  } else {
551cb0ef41Sopenharmony_ci    assert.notStrictEqual(promise, expected.shift());
561cb0ef41Sopenharmony_ci  }
571cb0ef41Sopenharmony_ci  assert.strictEqual(reason, expected.shift());
581cb0ef41Sopenharmony_ci}
59