dojo / core

:rocket: Dojo 2 - language helpers and utilities.
http://dojo.io
Other
213 stars 62 forks source link

Task: prevent finally w/canceled from resolving #264

Closed nicknisi closed 7 years ago

nicknisi commented 7 years ago

Prevent scenario where finally call after cancel results in a resolved Task.

Currently, Task#finally will return a new resolved Task when the task has already been cancelled. This leads to some inconsistencies where the Task returned by Finally will either be resolved or will not be resolvable, all depending on when cancel is called.

// This will return a new Task that is resolved to the return 
// value of the callback, allowing for further chaining of then calls
let task = new Task((resolve) => {
    setTimeout(resolve.bind(null, 'test'), 10);
});
task.cancel();
task = task.finally(() => 'changed');
task.then((value: string) => {
       // This callback will be called, resolving to the return value
       // of the finally call
    console.log(value); // changed
});

// However, when cancel is called after the finally callback is setup,
// the subsequent then call is never run.

let task2 = new Task((resolve) => {
    setTimeout(resolve.bind(null, 'test'), 10);
});
task2 = task2.finally(() => 'changed');
task2.then((value: string) => {
       // This is never resolved
    console.log(value);
});
task2.cancel();

This fixes the inconsistency by instead calling the callback passed to finally and then returning the same Task instance, which won't resolve if the Task has been cancelled.

jsf-clabot commented 7 years ago

CLA assistant check
All committers have signed the CLA.

codecov-io commented 7 years ago

Current coverage is 95.18% (diff: 100%)

Merging #264 into master will increase coverage by 0.49%

@@             master       #264   diff @@
==========================================
  Files            34         34          
  Lines          1828       1829     +1   
  Methods          19         19          
  Messages          0          0          
  Branches        354        354          
==========================================
+ Hits           1731       1741    +10   
+ Misses           34         27     -7   
+ Partials         63         61     -2   

Powered by Codecov. Last update bd37616...18ca9f1