diff --git a/Promise.js b/Promise.js index 7c04cd6..2001975 100644 --- a/Promise.js +++ b/Promise.js @@ -523,6 +523,20 @@ object.Constructor('IterablePromise', Promise, { // NOTE: if 'types/Array' is imported this will support throwing STOP, // for more info see notes for .__pack(..) // XXX EXPEREMENTAL: STOP... + // + // XXX BUG: + // await Promise.iter(['a', Promise.resolve(222), ['c']]) + // .map(async e => e) + // -> ['a', , ['c']] + // is not the same as: + // await Promise.iter(['a', Promise.resolve(222), ['c']]) + // .map(e => e) + // -> ['a', 222, ['c']] + // ...and these works as expected: + // await Promise.iter(['a', Promise.resolve(222), ['c']], async e => [e]) + // -> ['a', 222, ['c']] + // await Promise.iter(['a', Promise.resolve(222), ['c']], e => [e]) + // -> ['a', 222, ['c']] __new__: function(_, list, handler){ // instance... var promise diff --git a/generator.js b/generator.js index 9f60a2f..ecf27e5 100644 --- a/generator.js +++ b/generator.js @@ -66,6 +66,14 @@ module.Generator = (function*(){}).constructor +var AsyncGeneratorPrototype = + (async function*(){}).constructor.prototype + +var AsyncGenerator = +module.AsyncGenerator = + (async function*(){}).constructor + + // base iterator prototypes... var ITERATOR_PROTOTYPES = [ Array, @@ -428,6 +436,41 @@ ITERATOR_PROTOTYPES GeneratorProtoMixin(proto) }) +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// XXX EXPERIMENTAL... + +var makeAsyncGenerator = function(name, pre){ + return function(...args){ + var that = this + return Object.assign( + async function*(){ + var a = pre ? + pre.call(this, args, ...arguments) + : args + for await (var e of that(...arguments)[name](...a)){ + yield e } }, + { toString: function(){ + return [ + that.toString(), + // XXX need to normalize args better... + `.${ name }(${ args.join(', ') })`, + ].join('\n ') }, }) } } + +var AsyncGeneratorMixin = +module.AsyncGeneratorMixin = +object.Mixin('AsyncGeneratorMixin', 'soft', { + map: makeAsyncGenerator('map'), +}) + +var AsyncGeneratorProtoMixin = +module.AsyncGeneratorProtoMixin = +object.Mixin('AsyncGeneratorProtoMixin', 'soft', { +}) + +//AsyncGeneratorMixin(AsyncGeneratorPrototype) +//AsyncGeneratorProtoMixin(AsyncGeneratorPrototype.prototype) + + //--------------------------------------------------------------------- // Generators...