Concept inspired by the CancellationTokenSource
and CancellationToken
used in C# for cancelling async functions.
import CancellationTokenSource from './CancellationTokenSource'
async function doStuff(foo, cancellationToken) {
// do stuff with foo on each significant step call:
cancellationToken.throwIfCancelled();
// for example, my use-case is uploading chunks of a file and cancelling the XHR
return ':)';
}
var cst = new CancellationTokenSource;
doStuff('eee', cst.token)
.then(result => console.log('result', result))
.catch(err => console.error('failed', err));
setTimeout(() => cst.cancel(), 1000); // auto-cancel after one second
import CancellationTokenSource from './CancellationTokenSource'
function doStuff(foo, cancellationToken) {
return new Promise((resolve, reject) => {
// do stuff with foo on each significant step call:
if (cancellationToken.isCancelled()) return reject('Cancelled');
// for example, my use-case is uploading chunks of a file and cancelling the XHR
resolve(':)');
});
}
var cst = new CancellationTokenSource;
doStuff('eee', cst.token)
.then(result => console.log('result', result))
.catch(err => console.error('failed', err));
setTimeout(() => cst.cancel(), 1000); // auto-cancel after one second
OK, how to do something like this:
Backstory: Dialog appears asking the user to tap a NFC chip, user clicks back button and the dialog goes away. When the dialog appears it will run a "Promise writeMessage().then(...)" method which should be cancelled when going back.