@thrall/async-chain
Advanced tools
Comparing version
60
index.js
@@ -23,6 +23,3 @@ export function chain(target = global, strict = false, finalize = false) { | ||
this.proxy = new Proxy( | ||
/* target */ this, | ||
/* handler */ this | ||
); | ||
this.proxy = new Proxy(/* target */ this, /* handler */ this); | ||
@@ -35,9 +32,4 @@ this.reset(); | ||
// Trap for [[Get]] used by operations such as property accessors. | ||
get(target, property) { | ||
// console.log("[[Get]] " + property); | ||
if(property === "then" || | ||
property === "catch" || | ||
property === "finally") { | ||
get(target, property) { | ||
if (property === "then" || property === "catch" || property === "finally") { | ||
if (this.finalize) { | ||
@@ -65,8 +57,8 @@ // If the context has a finally method, run that before resolving the chain. | ||
this.reset(); | ||
return chain; | ||
} | ||
// Add the property or function to the chain. If it is a function, | ||
// this.apply will be called immediately after and will use the [ context, | ||
// Add the property or function to the chain. If it is a function, | ||
// this.apply will be called immediately after and will use the [ context, | ||
// callable ] tuple value this promise is returning. | ||
@@ -76,13 +68,28 @@ this.chain = this.chain.then((context) => { | ||
if (this.strict) { | ||
throw new Error(`Property not defined: ${property} (the` + | ||
` async-chain may have encountered an unexpected value)`); | ||
throw new Error( | ||
`Property not defined: ${property} (the` + | ||
` async-chain may have encountered an unexpected value)` | ||
); | ||
} else { | ||
if (context) { | ||
if (context.constructor) { | ||
console.warn( | ||
`Property not defined: ${context.constructor.name}.${property}` | ||
); | ||
} else { | ||
console.log(context.attr); | ||
console.warn( | ||
`Property not defined: ${context.name}[${property}]` | ||
); | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
switch (typeof context[property]) { | ||
case "function": | ||
return [ context, context[property] ]; | ||
return [context, context[property]]; | ||
default: | ||
@@ -103,3 +110,3 @@ case "boolean": | ||
// console.log("[[Call]]"); | ||
this.chain = this.chain.then(async (state) => { | ||
@@ -111,9 +118,12 @@ if (!Array.isArray(state)) { | ||
// The context and callee were returned because [[Get]] sent us a | ||
// The context and callee were returned because [[Get]] sent us a | ||
// callable. | ||
try { | ||
let [ context, callee ] = state; | ||
return await callee.apply(context, args); | ||
let [context, callee] = state; | ||
let result = await callee.apply(context, args); | ||
return result; | ||
} catch (e) { | ||
if(this.strict) { | ||
// console.log("TEST"); | ||
if (this.strict) { | ||
throw e; | ||
@@ -138,2 +148,2 @@ } | ||
} | ||
} | ||
} |
{ | ||
"name": "@thrall/async-chain", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "Chain asynchronous methods with await.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
11987
3.16%150
7.91%