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.all({[Symbol.iterator]() { return {get next() { throw 5; }}} 32}).then(onfullfilled => { 33 // If this is not called, the promise has failed, and catch must be called. 34 assert(false); 35}).catch(err => { 36 assert(err === 5); 37}); 38 39// iterator value 40Promise.all({ [Symbol.iterator] () { return { next () { return { get value () { throw 5 }}}}} 41}).then(onfullfilled => { 42 // If this is not called, the promise has failed, and catch must be called. 43 assert(false); 44}).catch(err => { 45 assert(err === 5); 46}); 47 48// iterator done 49Promise.all({ [Symbol.iterator] () { return { next () { return { get done () { throw 5 }}}}} 50}).then(onfullfilled => { 51 // If this is not called, the promise has failed, and catch must be called. 52 assert(false); 53}).catch(err => { 54 assert(err === 5); 55}); 56 57// iterator get 58Promise.all({ get [Symbol.iterator] () { throw 5 } 59}).then(onfullfilled => { 60 // If this is not called, the promise has failed, and catch must be called. 61 assert(false); 62}).catch(err => { 63 assert(err === 5); 64}); 65 66var fulfills = Promise.all(createIterable([ 67 new Promise(resolve => { resolve("foo"); }), 68 new Promise(resolve => { resolve("bar"); }), 69])); 70var rejects = Promise.all(createIterable([ 71 new Promise((_, reject) => { reject("baz"); }), 72 new Promise((_, reject) => { reject("qux"); }), 73])); 74 75fulfills.then(result => { assert (result + "" === "foo,bar"); }); 76rejects.catch(result => { assert (result === "baz"); }); 77 78var closed = false; 79delete Promise.resolve; 80Promise.all(createIterable([1,2,3], {'return': function () { closed = true; }})); 81assert (closed); 82