File: src/utils/setCancellableTimeout.js

Recommend this page to a friend!
  Classes of Jeremy Judeaux   Cancellable chain of promises   src/utils/setCancellableTimeout.js   Download  
File: src/utils/setCancellableTimeout.js
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Cancellable chain of promises
Run chains of actions based on asynchronous events
Author: By
Last change: Update of src/utils/setCancellableTimeout.js
Date: 3 years ago
Size: 627 bytes
 

Contents

Class file image Download
const setCancellableTimeout = (callback, delayParam = 0, tokenParam = undefined) => { let delay = delayParam; let token = tokenParam; if (arguments.length === 2 && typeof delayParam === 'object') { token = delayParam; delay = 0; } if (!token) { return setTimeout(callback, delay); } if (token.cancellationRequested) { return undefined; } let id; const registration = token.register(() => clearTimeout(id)); id = setTimeout(function onTimeout(...args) { registration.unregister(); callback.apply(this, args); }, delayParam); return id; }; export default setCancellableTimeout;