-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
You mention in the README that
This makes it easy to create Task sequences that can be cancelled.
How does that verbose and hard to learn syntax compare to just awaiting async functions in a loop, then simply breaking the loop in order "to create Task sequences that can be cancelled"?
For example, assume some tool called TaskRunner, written with ES2016 async/await in mind (all promise based):
// app.js -----------------------------------------------
import TaskRunner from './TaskRunner'
let tasks = new TaskRunner()
tasks.run([
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
async function() {/* ... */},
])
setTimeout(() => tasks.stop(), 1000) // stop the tasks in their tracks.
// TaskRunner.js -----------------------------------------------
export default
class TaskRunner {
constructor() {
this.shouldStop = false
}
async run(tasks) {
let cancelled = false
for (let i=0, len=tasks.length; i<len; i+=1) {
if (this.shouldStop) {
this.shouldStop = false
cancelled = true
break
}
else await tasks[i]()
}
return cancelled
}
stop() {
this.shouldStop = true
}
}That (basically) does what you said, but is a ton more readable (of course, features can be added like error handling, pausing tasks, keeping track of running state and percent complete, etc).
Maybe I'm not seeing the benefit of the Tasks yet (thanks for writing about them for me to read about in the first place 😄 ).
Metadata
Metadata
Assignees
Labels
No labels