Executes a Task and returns a TaskExecution
object representing the execution.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
Executes a Task and returns a TaskExecution
object representing the execution.
const { task } = require('folktale/concurrency/task');
let message = '';
const sayHello = task(resolver => {
message = 'hello';
resolver.resolve();
});
$ASSERT(message == '');
await sayHello.run().promise();
$ASSERT(message == 'hello');
run() {
let deferred = new Deferred(); // eslint-disable-line prefer-const
let cleanups = [];
let cancellations = [];
let isCancelled = false;
let done = false;
deferred.listen({
onCancelled: _ => {
done = true;
isCancelled = true;
cancellations.forEach(f => f());
cleanups.forEach(f => f());
cancellations = [];
cleanups = [];
},
onResolved: _value => {
done = true;
cleanups.forEach(f => f());
cleanups = [];
cancellations = [];
},
onRejected: _reason => {
done = true;
cleanups.forEach(f => f());
cleanups = [];
cancellations = [];
}
});
const execution = new TaskExecution(this, deferred);
const resources = this._computation({
reject: error => { deferred.reject(error) },
resolve: value => { deferred.resolve(value) },
cancel: _ => { deferred.maybeCancel() },
get isCancelled() { return isCancelled },
cleanup(f) {
if (done) {
throw new Error('Can\'t attach a cleanup handler after the task is settled.');
}
cleanups.push(f);
},
onCancelled(f) {
if (done) {
throw new Error('Can\'t attach a cancellation handler after the task is settled.');
}
cancellations.push(f);
}
});
return execution;
}