removed Array.prototype.promise() and more work on AsyngGenerator...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-06-21 13:41:06 +03:00
parent 81324a5fc6
commit a4d9a90f78
2 changed files with 26 additions and 20 deletions

View File

@ -102,7 +102,6 @@ Library of JavaScript type extensions, types and utilities.
- [`<generator>.flat(..)`](#generatorflat)
- [`<generator>.shift()` / `<generator>.pop()` / `<generator>.gshift()` / `<generator>.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop)
- [`<generator>.unshift(..)` / `<generator>.push(..)`](#generatorunshift--generatorpush)
- [`<generator>.promise()`](#generatorpromise)
- [`<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`](#generatorthen--generatorcatch--generatorfinally)
- [`<generator>.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 `<generator>` "first", i.e. on
_next_ call to `.next()`, regardless of the current generator state.
#### `<generator>.promise()`
#### `<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`
```bnf
<generator>.promise()
-> <promise>
```
Return a promise and resolve it with the generator value.
Note that this will deplete the generator.
```bnf
<generator>.then()
-> <promise>
```
#### `<generator>.then(..)` / `<generator>.catch(..)` / `<generator>.finally(..)`
Adding handlers to the promise
```bnf
<generator>.then(<resolve>, <reject>)
-> <promise>
<generator>.then(<reject>)
<generator>.then(<resolve>)
-> <promise>
<generator>.finally(<handler>)
-> <promise>
```
Shorthands to `<generator>.promise().then(..)` / `<generator>.promise().catch(..)` / `<generator>.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

View File

@ -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
// ...
})