@brainsights/promise-task
Advanced tools
Comparing version 2.2.0 to 2.2.1
36
index.js
@@ -12,3 +12,3 @@ const EventEmitter = require("events") | ||
{ | ||
constructor(task, resolve, reject) | ||
constructor(task, resolve, reject, options={}) | ||
{ | ||
@@ -19,2 +19,3 @@ __.set(this, { | ||
reject: reject, | ||
options: options, | ||
}); | ||
@@ -53,7 +54,13 @@ } | ||
__.get(task).onCancel = () => { | ||
if(cb) | ||
if(cb && _.isFunction(cb)) { | ||
cb.call(this); | ||
} | ||
this.reject(new Error("canceled")); | ||
} | ||
} | ||
get options() | ||
{ | ||
return __.get(this).options; | ||
} | ||
} | ||
@@ -77,3 +84,3 @@ | ||
executor(context) | ||
async executor(context) | ||
{ | ||
@@ -83,3 +90,3 @@ __.get(this).executor(context); | ||
run() | ||
run(options) | ||
{ | ||
@@ -92,11 +99,20 @@ if(!__.get(this).promise) { | ||
let promise = new Promise((resolve, reject) => { | ||
let context = new Context(this, resolve, reject); | ||
this.executor(context); | ||
let promise = new Promise(async (resolve, reject) => { | ||
let context = new Context(this, resolve, reject, options); | ||
await this.executor(context); | ||
}); | ||
__.get(this).promise = promise; | ||
return promise; | ||
} else { | ||
throw new Error("task is already running"); | ||
} | ||
} | ||
return __.get(this).promise; | ||
get promise() | ||
{ | ||
if(!__.get(this).promise) | ||
throw new Error("task is not started"); | ||
else | ||
return __.get(this).promise; | ||
} | ||
@@ -149,7 +165,7 @@ | ||
canceled = true; | ||
task.cancel | ||
task.cancel(); | ||
}; | ||
try { | ||
results[i] = await task.run(); | ||
results[i] = await task.run(context.options); | ||
} catch(e) { | ||
@@ -156,0 +172,0 @@ return context.reject(e); |
{ | ||
"name": "@brainsights/promise-task", | ||
"version": "2.2.0", | ||
"version": "2.2.1", | ||
"description": "Lazy promises with support for cancellation and progress events", | ||
@@ -17,4 +17,4 @@ "main": "index.js", | ||
"@brainsights/uuid": "1.1.0", | ||
"lodash": "^4.17.5" | ||
"lodash": "4.17.5" | ||
} | ||
} |
5649
155
+ Addedlodash@4.17.5(transitive)
- Removedlodash@4.17.21(transitive)
Updatedlodash@4.17.5