@webreflection/lie
Advanced tools
Comparing version 0.1.1 to 1.0.0
'use strict'; | ||
module.exports = typeof Promise === 'function' ? Promise : function (fn) { | ||
let queue = [], resolved = 0, value; | ||
fn($ => { | ||
value = $; | ||
resolved = 1; | ||
queue.splice(0).forEach(then); | ||
}); | ||
return {then}; | ||
function then(fn) { | ||
return (resolved ? setTimeout(fn, 0, value) : queue.push(fn)), this; | ||
class Lie { | ||
/** @type {unknown} */ | ||
#value; | ||
/** | ||
* @param {unknown} value | ||
*/ | ||
constructor(value) { | ||
this.#value = value; | ||
} | ||
}; | ||
/** | ||
* Directly invoke the `success` callback with the non-`Promise` value. | ||
* @param {(value: unknown) => unknown} success | ||
* @param {function} [_] ignored `failure` callback | ||
* @returns {Promise<unknown> | Lie<unknown>} | ||
*/ | ||
then(success, _) { | ||
return lie(success(this.#value)); | ||
} | ||
/** | ||
* It ignores the `failure` callback and returns `this` lie. | ||
* @param {function} _ ignored `failure` callback | ||
* @returns | ||
*/ | ||
catch(_) { | ||
return this; | ||
} | ||
} | ||
/** | ||
* | ||
* @param {Promise<unknown> | unknown} value | ||
* @returns {Promise<unknown> | Lie<unknown>} | ||
*/ | ||
const lie = value => value instanceof Promise ? value : new Lie(value); | ||
module.exports = lie; |
@@ -1,12 +0,39 @@ | ||
export default typeof Promise === 'function' ? Promise : function (fn) { | ||
let queue = [], resolved = 0, value; | ||
fn($ => { | ||
value = $; | ||
resolved = 1; | ||
queue.splice(0).forEach(then); | ||
}); | ||
return {then}; | ||
function then(fn) { | ||
return (resolved ? setTimeout(fn, 0, value) : queue.push(fn)), this; | ||
class Lie { | ||
/** @type {unknown} */ | ||
#value; | ||
/** | ||
* @param {unknown} value | ||
*/ | ||
constructor(value) { | ||
this.#value = value; | ||
} | ||
}; | ||
/** | ||
* Directly invoke the `success` callback with the non-`Promise` value. | ||
* @param {(value: unknown) => unknown} success | ||
* @param {function} [_] ignored `failure` callback | ||
* @returns {Promise<unknown> | Lie<unknown>} | ||
*/ | ||
then(success, _) { | ||
return lie(success(this.#value)); | ||
} | ||
/** | ||
* It ignores the `failure` callback and returns `this` lie. | ||
* @param {function} _ ignored `failure` callback | ||
* @returns | ||
*/ | ||
catch(_) { | ||
return this; | ||
} | ||
} | ||
/** | ||
* | ||
* @param {Promise<unknown> | unknown} value | ||
* @returns {Promise<unknown> | Lie<unknown>} | ||
*/ | ||
const lie = value => value instanceof Promise ? value : new Lie(value); | ||
export default lie; |
{ | ||
"name": "@webreflection/lie", | ||
"version": "0.1.1", | ||
"description": "A badly implemented Promise", | ||
"version": "1.0.0", | ||
"description": "An optionally sync promise that directly passes along its value", | ||
"main": "./cjs/index.js", | ||
"scripts": { | ||
"build": "ascjs --no-default esm cjs" | ||
"build": "ascjs --no-default esm cjs && npm test", | ||
"test": "node test/index.js" | ||
}, | ||
"keywords": [ | ||
"Promise" | ||
"Promise", | ||
"lie", | ||
"direct", | ||
"sync" | ||
], | ||
"author": "Andrea Giammarchi", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"ascjs": "^4.0.1" | ||
"ascjs": "^6.0.3" | ||
}, | ||
@@ -23,3 +27,2 @@ "module": "./esm/index.js", | ||
}, | ||
"unpkg": "es.js", | ||
"repository": { | ||
@@ -26,0 +29,0 @@ "type": "git", |
# Lie | ||
A badly implemented Promise | ||
An optionally sync promise that directly passes along its value, particularly useful for any *API* that handles both synchronous and asynchronous use cases through explicit `.then(...)` invocation. | ||
```js | ||
import Lie from '@webreflection/lie'; | ||
lie(123) | ||
// invoked synchronously | ||
.then(value => { | ||
console.log('sync #1', value); | ||
return value * 2; | ||
}) | ||
// invoked synchronously | ||
// because the result is an async function | ||
// the returned value will be a Promise this time | ||
.then(async value => { | ||
console.log('sync #2', value); | ||
return value * 2; | ||
}) | ||
// this time is a Promise | ||
.then(value => { | ||
console.log('async #1', value); | ||
}) | ||
; | ||
const lie = new Lie(resolve => { | ||
// resolve only ... | ||
}).then(() => { | ||
// ... and no catch method | ||
}); | ||
console.log('sync #1 and #2 already logged'); | ||
// async #1 going to be logged after | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
4306
6
69
0
28