1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// Copyright 2015 the V8 project authors. All rights reserved. 17// Use of this source code is governed by a BSD-style license that can be 18// found in the LICENSE file. 19 20// Test that Promises use @@species appropriately 21 22// Another constructor with no species will not be instantiated 23 24var test = new Promise(function(){}); 25var bogoCount = 0; 26function bogusConstructor() { bogoCount++; } 27test.constructor = bogusConstructor; 28assert(Promise.resolve(test) instanceof Promise); 29assert(!(Promise.resolve(test) instanceof bogusConstructor)); 30 31// If there is a species, it will be instantiated 32// @@species will be read exactly once, and the constructor is called with a 33// function 34var count = 0; 35var params; 36 37class MyPromise extends Promise { 38 constructor(...args) { 39 super(...args); 40 params = args; 41 } 42 static get [Symbol.species]() { 43 count++ 44 return this; 45 } 46} 47 48var myPromise = MyPromise.resolve().then(); 49assert(1 === count); 50assert(1 === params.length); 51assert('function' === typeof(params[0])); 52assert(myPromise instanceof MyPromise); 53assert(myPromise instanceof Promise); 54