File: src/utils/cancelPreviousCalls.js

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

Contents

Class file image Download
import { always } from '../index'; const cancelPreviousCalls = (builder, CancellationTokenSource) => { let previousSource = null; return function wrapper(...arts) { if (previousSource) { previousSource.cancel(); } const source = new CancellationTokenSource(); previousSource = source; const func = builder(source.token); const result = func.apply(this, arts); always.call(Promise.resolve(result), () => { if (previousSource === source) { previousSource = null; } }); return result; }; }; export default cancelPreviousCalls;