All Projects → dondevi → promise-abortable

dondevi / promise-abortable

Licence: MIT license
Promise lib for aborting in chain.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to promise-abortable

sigctx
context with signal in golang
Stars: ✭ 19 (+0%)
Mutual labels:  signal, cancel
wtsqs
Simplified Node AWS SQS Worker Wrapper
Stars: ✭ 18 (-5.26%)
Mutual labels:  promise
redux-reducer-async
Create redux reducers for async behaviors of multiple actions.
Stars: ✭ 14 (-26.32%)
Mutual labels:  promise
etsy-ts
Etsy API wrapper written in typescript
Stars: ✭ 18 (-5.26%)
Mutual labels:  promise
vue2-element
基于vue2 + vue-router2 + element-ui + vuex2 + fetch + webpack2 企业级后台管理系统最佳实践
Stars: ✭ 115 (+505.26%)
Mutual labels:  promise
mongoose-aggregate-paginate-v2
A cursor based custom aggregate pagination library for Mongoose with customizable labels.
Stars: ✭ 103 (+442.11%)
Mutual labels:  promise
Fetch
Asynchronous HTTP client with promises.
Stars: ✭ 29 (+52.63%)
Mutual labels:  promise
nodeJS examples
Server, routing, db examples using NodeJS v6
Stars: ✭ 34 (+78.95%)
Mutual labels:  promise
miniprogram-network
Redefine the Network API of Wechat MiniProgram (小程序网络库)
Stars: ✭ 93 (+389.47%)
Mutual labels:  promise
nancy
How JavaScript Promise Works
Stars: ✭ 26 (+36.84%)
Mutual labels:  promise
ngx-localstorage
An Angular wrapper for localstorage/sessionstorage access.
Stars: ✭ 27 (+42.11%)
Mutual labels:  promise
async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+447.37%)
Mutual labels:  promise
do
Simplest way to manage asynchronicity
Stars: ✭ 33 (+73.68%)
Mutual labels:  promise
woodpecker
woodpecker http client for Android
Stars: ✭ 17 (-10.53%)
Mutual labels:  promise
co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (-10.53%)
Mutual labels:  promise
YACLib
Yet Another Concurrency Library
Stars: ✭ 193 (+915.79%)
Mutual labels:  promise
mst-effect
💫 Designed to be used with MobX-State-Tree to create asynchronous actions using RxJS.
Stars: ✭ 19 (+0%)
Mutual labels:  promise
django-sqlalchemy
Django ORM built on top of SQLalchemy core 2.0 for seamless integration of SQLAlchemy with Django 4.1+ PostgreSQL 14+ only for now. [pre POC now]
Stars: ✭ 101 (+431.58%)
Mutual labels:  signal
PiedPiper
A small set of classes and functions to make easy use of Futures, Promises and async computation in general. All written in Swift for iOS 10+, WatchOS 3, tvOS and Mac OS X apps.
Stars: ✭ 44 (+131.58%)
Mutual labels:  promise
PromisedFuture
A Swift based Future/Promises framework to help writing asynchronous code in an elegant way
Stars: ✭ 75 (+294.74%)
Mutual labels:  promise

promise-abortable

NPM version node version build status Test coverage Install size NPM download NPM count License

Concept

abort != reject

Features

  • Abort in promise
  • Abort in promise chain
  • Abort for nesting promise
  • Return promise after abort

Use Cases

  • Cancel request when component hide, unmount or destory
  • Cancel long-running async operation
  • Return promise with abort for common request function

Browser Support

Any browser that supports Promise.

Chrome Firefox Safari Opera Edge
33 29 8 20 12
  • Use Babel for lower versions
  • Or include script iife.es3.js below
  • But I think bluebird 3 is a better choice

Install

$ npm install promise-abortable
import AbortablePromise from "promise-abortable";

// For Node 6+
const AbortablePromise = require("promise-abortable");

// For Node 4-
var AbortablePromise = require("promise-abortable/dist/cjs.es5.js");

The IIFE build is also available on unpkg:

<script src="https://unpkg.com/promise-abortable/dist/iife.es5.js"></script> <!-- 1KB, recommend -->
<script src="https://unpkg.com/promise-abortable/dist/iife.es6.js"></script> <!-- 1KB -->
<script src="https://unpkg.com/promise-abortable/dist/iife.es3.js"></script> <!-- 16KB -->

Usage

// 1. Instantiate
const promise = new AbortablePromise((resolve, reject, signal) => {
  // 2. Set abort handler
  signal.onabort = reason => {
    // 4. Abort won't reject, but you can reject manually
  };
});
// 3. Invoke `signal.onabort(reason)`
promise.abort(reason);

Pseudo code

See full examples here.

Abort in promise

const promise = new AbortablePromise(...);
// or: const promise = AbortablePromise.resolve(...);
// or: const promise = AbortablePromise.reject(...);
// or: const promise = AbortablePromise.all([...]);
// or: const promise = AbortablePromise.race([...]);
promise.abort();

Abort in promise chain

const promise = new AbortablePromise(...).then(...).catch(...);
promise.abort();

Abort for nesting promise

const promise = AbortablePromise.resolve(...).then(value => {
  return new AbortablePromise(...);
});
promise.abort();

Return promise after abort

const promise = new AbortablePromise(...);
promise.abort().then(...).catch(...);

Abort in async/await

const promise = new AbortablePromise(...);
(async () => {
  try { await promise; } catch (error) {...}
})();
promise.abort();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].