-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pause/resume methods; Added allSettled method; Removed CPromiseScope; Refactored;
- Loading branch information
1 parent
ee002ea
commit 25e2b0a
Showing
7 changed files
with
381 additions
and
1,574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const CPromise= require('../lib/c-promise'); | ||
|
||
function cancelableDelay(ms, value){ | ||
return new CPromise(function(resolve, reject, {onCancel, onPause, onResume}){ | ||
let timestamp= Date.now(); | ||
let timeLeft; | ||
let timer= setTimeout(resolve, ms, value); | ||
onPause(()=>{ | ||
console.log(`Pause`); | ||
clearTimeout(timer); | ||
timer=0; | ||
timeLeft= ms - (Date.now()- timestamp); | ||
timestamp= Date.now(); | ||
}); | ||
|
||
onResume(()=>{ | ||
console.log(`Resume`); | ||
timer= setTimeout(resolve, timeLeft, value); | ||
}); | ||
|
||
onCancel(()=>{ | ||
console.log(`Cancel`); | ||
timer && clearTimeout(timer); | ||
}) | ||
}); | ||
} | ||
|
||
const chain= cancelableDelay(1000, 123) | ||
.then( | ||
value=> console.log(`Done:`, value), | ||
err=> console.warn(`Fail: ${err}`) | ||
); | ||
|
||
setTimeout(()=>{ | ||
chain.pause(); | ||
|
||
setTimeout(()=>{ | ||
chain.resume(); | ||
}, 5000); | ||
}, 100); | ||
|
Oops, something went wrong.