From ea6fbc1ff5a626b9a96c6b5ae53a88b9520b14d7 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 23 Nov 2020 22:27:30 +0300 Subject: [PATCH] fixed a logical design error... Signed-off-by: Alex A. Naanou --- Array.js | 12 ++++++------ README.md | 36 ++++++++++++++++++------------------ package.json | 2 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Array.js b/Array.js index 946c6ca..0246540 100644 --- a/Array.js +++ b/Array.js @@ -19,7 +19,7 @@ var generator = require('./generator') /*********************************************************************/ -// NOTE: this is used in a similar fasion to Python's StopIteration... +// NOTE: this is used in a similar fashion to Python's StopIteration... var STOP = module.STOP = object.STOP @@ -80,7 +80,7 @@ var wrapIterFunc = function(iter){ // // // STOP can be thrown in func or chunk_handler at any time to -// abort iteration, this will reject the promise. +// abort iteration, this will resolve the promise. // // // The main goal of this is to not block the runtime while processing a @@ -128,9 +128,9 @@ var makeChunkIter = function(iter, wrapper){ // handle STOP... } catch(err){ if(err === STOP){ - return Promise.reject() + return Promise.resolve() } else if( err instanceof STOP){ - return Promise.reject(err.value) } + return Promise.resolve(err.value) } throw err } } var res = [] @@ -153,9 +153,9 @@ var makeChunkIter = function(iter, wrapper){ // handle STOP... } catch(err){ if(err === STOP){ - return reject() + return resolve() } else if( err instanceof STOP){ - return reject(err.value) } + return resolve(err.value) } throw err } // stop condition... diff --git a/README.md b/README.md index 7afe2f9..5cfa0fc 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,10 @@ A library of JavaScript type extensions, types and type utilities. - [`Array.zip(..)` / `.zip(..)`](#arrayzip--arrayzip) - [`Array.iter(..)` / `.iter()`](#arrayiter--arrayiter) - [Abortable `Array` iteration](#abortable-array-iteration) - - [`array.StopIteration`](#arraystopiteration) + - [`array.STOP` / `array.STOP(..)`](#arraystop--arraystop) - [`.smap(..)` / `.sfilter(..)` / `.sreduce(..)` / `.sforEach(..)`](#arraysmap--arraysfilter--arraysreduce--arraysforeach) - [Large `Array` iteration (chunked)](#large-array-iteration-chunked) - - [`array.StopIteration`](#arraystopiteration-1) + - [`array.STOP` / `array.STOP(..)`](#arraystop--arraystop-1) - [`.CHUNK_SIZE`](#arraychunk_size) - [`.mapChunks(..)`](#arraymapchunks) - [`.filterChunks(..)`](#arrayfilterchunks) @@ -417,10 +417,10 @@ This is mostly useful in combination with the [Generator extensions and utilitie ### Abortable `Array` iteration A an alternative to `Array`'s `.map(..)` / `.filter(..)` / .. methods with ability to -stop the iteration process by `throw`ing `StopIteration`. +stop the iteration process by `throw`ing `STOP` or `STOP()`. ```javascript -var {StopIteration} = require('ig-types/Array') +var {STOP} = require('ig-types/Array') ``` This can be used in two ways: @@ -430,35 +430,35 @@ This can be used in two ways: ;[1,2,3,4,5] .smap(function(e){ // simply abort here and now... - throw StopIteration }) + throw STOP }) ``` - Since we aborted the iteration without passing any arguments to `StopIteration`, + Since we aborted the iteration without passing any arguments to `STOP`, `.smap(..)` will return `undefined`. 2) `throw` an instance and return the argument... ```javascript - // this will print "4" -- the value passed to StopIteration... + // this will print "4" -- the value passed to STOP... console.log([1,2,3,4,5] .smap(function(e){ if(e > 3){ // NOTE: new is optional here... // ...StopIteratiom is an object.js constructor. - throw new StopIteration(e) } })) + throw new STOP(e) } })) ``` -Note that no partial result is returned unless passed through `StopIteration(..)`. +Note that no partial result is returned unless passed through `STOP(..)`. -#### `array.StopIteration` +#### `array.STOP` / `array.STOP(..)` -An exception that if raised while iterating via a supporting iterator method -will abort further execution and correctly exit. +An _object/constructor_ that if raised (as an exception) while iterating via +a supporting iterator method will abort further execution and correctly exit. #### `.smap(..)` / `.sfilter(..)` / `.sreduce(..)` / `.sforEach(..)` Like `Array`'s `.map(..)`, `.filter(..)`, `.reduce(..)` and `.forEach(..)` but -with added support for aborting iteration by throwing `StopIteration`. +with added support for aborting iteration by throwing `STOP` or `STOP()`. ### Large `Array` iteration (chunked) @@ -495,18 +495,18 @@ var c = await [1,2,3,4,5] return e*2 }) ``` -#### `array.StopIteration` +#### `array.STOP` / `array.STOP(..)` Like for [`.smap(..)` and friends](#abortable-array-iteration) iteration -can be stopped by throwing a `array.StopIteration` and as before there are -two ways to go: +can be stopped by throwing a `array.STOP` / `array.STOP()` and as before +there are two ways to go: 1) `throw` as-is to simply stop ```javascript ;[1,2,3,4,5] .mapChunks(function(e){ // simply abort here and now... - throw StopIteration }) + throw STOP }) .catch(function(){ console.log('done.') }) ``` @@ -518,7 +518,7 @@ two ways to go: if(e > 3){ // NOTE: new is optional here... // ...StopIteratiom is an object.js constructor. - throw new StopIteration(e) } }) + throw new STOP(e) } }) .catch(function(e){ console.log('first value greater than 3:', e) }) ``` diff --git a/package.json b/package.json index 30a463c..f82cdd1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "4.1.3", + "version": "5.0.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {