From a4d9a90f7852537c0e14b0499353c6fdcf4c2ea0 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 21 Jun 2022 13:41:06 +0300 Subject: [PATCH] removed Array.prototype.promise() and more work on AsyngGenerator... Signed-off-by: Alex A. Naanou --- README.md | 20 +++++++++----------- generator.js | 26 +++++++++++++++++--------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 72aaa65..aef5637 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,6 @@ Library of JavaScript type extensions, types and utilities. - [`.flat(..)`](#generatorflat) - [`.shift()` / `.pop()` / `.gshift()` / `.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop) - [`.unshift(..)` / `.push(..)`](#generatorunshift--generatorpush) - - [`.promise()`](#generatorpromise) - [`.then(..)` / `.catch(..)` / `.finally(..)`](#generatorthen--generatorcatch--generatorfinally) - [`.toArray()`](#generatortoarray) - [Treating iterators the same as generators](#treating-iterators-the-same-as-generators) @@ -2157,30 +2156,29 @@ Value added by `.unshift(..)` will be yielded by `` "first", i.e. on _next_ call to `.next()`, regardless of the current generator state. -#### `.promise()` +#### `.then(..)` / `.catch(..)` / `.finally(..)` -```bnf -.promise() - -> -``` Return a promise and resolve it with the generator value. -Note that this will deplete the generator. +```bnf +.then() + -> +``` - -#### `.then(..)` / `.catch(..)` / `.finally(..)` +Adding handlers to the promise ```bnf .then(, ) -> -.then() +.then() -> .finally() -> ``` -Shorthands to `.promise().then(..)` / `.promise().catch(..)` / `.promise().finally(..)` + +Note that this will deplete the generator. These are the same as equivalent `Promise` methods, for more info see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise diff --git a/generator.js b/generator.js index 695ab6f..f91f25b 100644 --- a/generator.js +++ b/generator.js @@ -407,17 +407,19 @@ object.Mixin('GeneratorProtoMixin', 'soft', { // promises... // - // XXX how do we handle reject(..) / .catch(..)??? - promise: function(){ + then: function(onresolve, onreject){ var that = this - return new Promise(function(resolve){ - resolve([...that]) }) }, - then: function(func){ - return this.promise().then(func) }, + var p = new Promise( + function(resolve){ + resolve([...that]) }) + p = (onresolve || onreject) ? + p.then(...arguments) + : p + return p }, catch: function(func){ - return this.promise().catch(func) }, + return this.then().catch(func) }, finally: function(func){ - return this.promise().finally(func) }, + return this.then().finally(func) }, // combinators... // @@ -510,6 +512,12 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', { return [] }) return state }, + // XXX TEST... + chain: async function*(...next){ + yield* next + .reduce(function(cur, next){ + return next(cur) }, this) }, + flat: async function*(){ for await(var e of this){ if(e instanceof Array){ @@ -529,7 +537,7 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', { // XXX // slice - // flat + // chain // ... })