Code Monkey home page Code Monkey logo

axiospromise's Introduction

Axios Promise

Lightweight Promises/A+ compatible implementation with cancellation, sync mode, timeouts, coroutines and signals support

npm version Build status Coverage Status npm bundle size npm downloads

Installing

Using npm:

$ npm install axios-promise
import {AxiosPromise, AxiosPromiseSync} from 'axios-promise';

Intro

AxiosPromise is a Promises/A+ compatible implementation with cancellation API extension, therefore, to start using it, you do not need to additionally learn a new and complex API. All you need to know that every promise:

  • has .cancel([reason]) method
  • has .onCancel subscriber for optional aborting long-running asynchronous operation inside a promise

Basic Examples

Live demo

import { AxiosPromise } from 'axios-promise';

const p = new AxiosPromise((resolve, reject, {onCancel}) => {
  const timer = setTimeout(resolve, 1000, 123);
  onCancel((reason) => {
    console.log('clear timer', reason);
    clearTimeout(timer);
  });
}).then(
  v => console.log(`Done: ${v}`), 
  e => console.warn(`Fail: ${e}`)
);

setTimeout(()=> p.cancel(), 500);

Instead of using the plain onCancel listener you can subscribe to a AbortSignal instance for the same purpose.

You can also set onCancel handler by returning it from the promise executor function:

Live demo

import { AxiosPromise } from 'axios-promise';

const p = new AxiosPromise((resolve) => {
  const timer = setTimeout(resolve, 1000, 123);
  return (reason) => {
    console.log('clear timer', reason);
    clearTimeout(timer);
  };
}).then(
  v => console.log(`Done: ${v}`), 
  e => console.warn(`Fail: ${e}`)
);

setTimeout(()=> p.cancel(), 500);

See Live Playground (Version for Node)

function cancelableFetch(url) {
    return new AxiosPromise((resolve, reject, {signal}) => {
        fetch(url, {signal}).then(resolve, reject)
    });
}

const p = cancelableFetch('http://httpbin.org/get').then((response) => response.json()).then(console.log);

// p.cancel();

OR

const p = cancelableFetch('http://httpbin.org/get').then(function*(response) {
  const json = yield response.json();
  console.log(json);
});

OR

const requestJSON = AxiosPromise.promisify(function*({signal}, url) {
  const response = yield fetch(url, {signal});
  const json = yield response.json();
  console.log(json);
}, {scopeArg: true});

const p = requestJSON('http://httpbin.org/get');

// p.cancel();

Features

  • no dependencies, less than 5KB gzipped

  • provides two classes:

    • AxiosPromise - Promises/A+ compatible implementation
    • AxiosPromiseSync - a subclass of AxiosPromise but with synchronous chains resolution
  • recursive cancellation by rejecting the deepest promise in the chain with a special CanceledError reason

    Note: AxiosPromise passes an additional scope argument to the executor function and then handlers, which refers to the promise context.

    const p = new AxiosPromise((resolve, reject, scope) => {
      const timer = setTimeout(resolve, 1000);
      scope.onCancel(() => clearTimeout(timer));
    }).then((value, scope) => console.log(value), (reason, scope) => console.warn(reason));
    
    setTimeout(() => p.cancel(), 100);
  • cancellable delay helper

    await AxiosPromise.delay(1000, 'foo');
  • timeouts - each promise has a timeout setter

    await AxiosPromise.delay(1000).timeout(100);
  • subscribing to an external AbortSignal

    const controller = new AbortController();
    AxiosPromise.delay(1000).listen(controller.signal).then(console.log, console.warn);
    setTimeout(() => controller.abort(), 100);
  • Providing an AbortSignal to subscribe - every promise has a signal getter that you can subscribe to

    AxiosPromise.delay(1000).timeout(100).signal.addEventListener('abort', () => console.log('canceled'));
  • atomic chains - makes sub-chain not cancellable when canceled from upper chain

    • AWAIT = false mode (default value) - waits for the atomic sub-chain to resolve before canceling the upper chain

      const p = AxiosPromise.delay(1000).atomic(false).then(() => AxiosPromise.delay(1000)).then(console.log, console.warn);
      
      p.cancel(); // will wait for the first promise and then cancel the rest
    • DETACHED = true mode - does not wait for the atomic sub-chain to resolve, but cancels the upper chain.

      const p = AxiosPromise.delay(1000).atomic(true).then(() => AxiosPromise.delay(1000)).then(console.log, console.warn);
      
      p.cancel(); // will keep the first promise pending and cancel the rest of the chain
  • coroutines - write flat cancelable code as if you were using ECMA asynchronous functions

    const myAsyncFunc = AxiosPromise.promisify(function*(str) {
      const newStr = yield AxiosPromise.delay(1000, 'Hello ') + str;
      return newStr;
    });
    
    const p = myAsyncFunc(123).then(function*(value) {
      yield AxiosPromise.delay(1000);
      console.log(`It supports generators in then method, result is:`, value);
    });
    
    //p.cancel();
  • .all method will cancel all pending promises if one rejected

  • .race method will cancel all pending promises if one resolved

  • finally method handles a status entry as the first argument

Error handling

Cancelling chain means rejecting its deepest promise with a CanceledError reason.

const p = new AxiosPromise((resolve, reject, {onCancel}) => {
  const timer = setTimeout(()=> {
    Math.random() > 0.5 ? reject(new Error('Oops!')) : resolve('foo');
  }, 1000);
  onCancel(() => clearTimeout(timer));
}).then((value) => {
  return value + 'bar';
}).catch((err) => {
  // re-throw the error if this is an instance of CanceledError to handle it with upper chains
  CanceledError.rethow(err);
  console.log('Error', err)
});

Note: uncaught CanceledError rejection won't lead to unhandledrejection warning

Helpers

bottleneck

A helper that creates a simple queue by decorating an asynchronous function.

bottleneck(fn, options?: {concurrency: number, cancelRunning: boolean, sync: boolean, timeout: number, taskTimeout: number, queueTimeout: number})

  const fn = bottleneck(function* () {
    console.log('start');
    yield AxiosPromise.delay(2000);
    console.log('end');
    return 'foo'
  }, {concurrency: 1});

  const results = await Promise.allSettled([
    fn(),
    fn(),
    fn()
  ]);

  console.log(results.map(({status, reason, value}) => status + ' : ' + (reason?.toString() || value)))

Log:

start
end
start
end
start
end
[ 'fulfilled : foo', 'fulfilled : foo', 'fulfilled : foo' ]

Options

cancelRunning - cancel running tasks if the concurrency limit is reached

const fn = bottleneck(function* () {
  console.log('start');
  yield AxiosPromise.delay(2000);
  console.log('end');
  return 'foo'
}, {cancelRunning: true, concurrency: 1});

const results = await Promise.allSettled([
  fn(),
  fn(),
  fn()
]);

console.log(results.map(({status, reason, value}) => status + ' : ' + (reason?.message || value)))

Log:

start
end
[
  'rejected : CanceledError: task limit reached',
  'rejected : CanceledError: task limit reached',
  'fulfilled : foo'
]

timeout/taskTimeout/queueTimeout - use AxiosPromiseSync instead AxiosPromise

Sets appropriate timeouts

(async () => {
  const fn = bottleneck(function* (t = 2000) {
    console.log('start');
    yield AxiosPromise.delay(t);
    console.log('end');
    return 'foo'
  }, {concurrency: 1, taskTimeout: 1000});

  const results = await Promise.allSettled([
    fn(),
    fn(),
    fn(500)
  ]);

  console.log(results.map(({status, reason, value}) => status + ' : ' + (reason?.toString() || value)))
})();

Log:

start
start
start
end
[
  'rejected : TimeoutError: task timeout',
  'rejected : TimeoutError: task timeout',
  'fulfilled : foo'
]

Manual cancellation

  const fn = bottleneck(function* (t = 2000) {
    console.log('start');
    yield AxiosPromise.delay(t);
    console.log('end');
    return 'foo'
  }, {concurrency: 1});

  const tasks = [
    fn(),
    fn(),
    fn()
  ];

  setTimeout(() => {
    tasks[1].cancel('Oops!');
  }, 500);

  const results = await Promise.allSettled(tasks);

  console.log(results.map(({status, reason, value}) => status + ' : ' + (reason?.toString() || value)))

Log:

start
end
start
end
[
  'fulfilled : foo',
  'rejected : CanceledError: Oops!',
  'fulfilled : foo'
]

License

The MIT License Copyright (c) 2023 Dmitriy Mozgovoy [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

axiospromise's People

Contributors

digitalbrainjs avatar github-actions[bot] avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.