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 16function createIterable(arr, methods = {}) { 17 let iterable = function *() { 18 let idx = 0; 19 while (idx < arr.length) { 20 yield arr[idx]; 21 idx++; 22 } 23 }(); 24 iterable['return'] = methods['return']; 25 iterable['throw'] = methods['throw']; 26 27 return iterable; 28}; 29 30// iterator next 31Promise.race({[Symbol.iterator]() { return {get next() { throw 5; }}} 32}).catch(err => { 33 assert(err === 5); 34}); 35 36// iterator value 37Promise.race({ [Symbol.iterator] () { return { next () { return { get value () { throw 5 }}}}} 38}).catch(err => { 39 assert(err === 5); 40}); 41 42// iterator done 43Promise.race({ [Symbol.iterator] () { return { next () { return { get done () { throw 5 }}}}} 44}).catch(err => { 45 assert(err === 5); 46}); 47 48// iterator get 49Promise.race({ get [Symbol.iterator] () { throw 5 } 50}).catch(err => { 51 assert(err === 5); 52}); 53 54var fulfills = Promise.race(createIterable([ 55 new Promise(resolve => { resolve("foo"); }), 56 new Promise(resolve => { resolve("bar"); }), 57])); 58var rejects = Promise.race(createIterable([ 59 new Promise((_, reject) => { reject("baz"); }), 60 new Promise((_, reject) => { reject("qux"); }), 61])); 62 63fulfills.then(result => { assert (result + "" === "foo"); }); 64rejects.catch(result => { assert (result === "baz"); }); 65 66var closed = false; 67delete Promise.resolve; 68Promise.race(createIterable([1,2,3], {'return': function () { closed = true; }})); 69assert (closed); 70