Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-thunk

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-thunk

promise thunk

  • 0.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
138
decreased by-0.72%
Maintainers
1
Weekly downloads
 
Created
Source

promise-thunk - npm

ES6 Promise + Thunk = PromiseThunk!

promise-thunk is standard ES6 Promise implementation + thunk.
it supports node.js/io.js.

it throws unhandled rejection error.

INSTALL:

for node.js or io.js

$ npm install promise-thunk --save

or

for browsers

https://lightspeedworks.github.io/promise-thunk/promise-thunk.js

<script src="https://lightspeedworks.github.io/promise-thunk/promise-thunk.js"></script>

PREPARE:

you can use PromiseThunk.

(function (PromiseThunk) {
  'use strict';
  // you can use PromiseThunk
})(this.PromiseThunk || require('promise-thunk'));

or

var PromiseThunk = this.PromiseThunk || require('promise-thunk');

QUICK EXAMPLE:

var PromiseThunk = require('promise-thunk');

function sleepNodeStyle(msec, val, cb) {
  console.log(val + ' timer start: ' + msec);
  setTimeout(function () {
    console.log(val + ' timer end  : ' + msec);
    cb(null, val);
  }, msec);
}

var sleepPromiseThunk = PromiseThunk.wrap(sleepNodeStyle);

// promise type
sleepPromiseThunk(1000, 'a1')
.then(
  function (val) {
    console.log('a2 val: ' + val);
    return sleepPromiseThunk(1000, 'a2'); },
  function (err) { console.log('err:' + err); })
.catch(
  function (err) { console.log('err:' + err); }
);

// thunk type
sleepPromiseThunk(1000, 'b1')
(function (err, val) {
  console.log('b2 val: ' + val + (err ? ' err: ' + err : ''));
  return sleepPromiseThunk(1000, 'b2'); })
(function (err, val) {
  console.log('b3 val: ' + val + (err ? ' err: ' + err : '')); });

// mixed promise and thunk type
sleepPromiseThunk(1000, 'c1')
(function (err, val) {
  console.log('c2 val: ' + val + (err ? ' err: ' + err : ''));
  return sleepPromiseThunk(1000, 'c2'); })
.then(
  function (val) {
    console.log('c3 val: ' + val);
    return sleepPromiseThunk(1000, 'c3'); },
  function (err) { console.log('err:' + err); });

//output:
// a1 timer start: 1000
// b1 timer start: 1000
// c1 timer start: 1000
// a1 timer end  : 1000
// a2 val: a1
// a2 timer start: 1000
// b1 timer end  : 1000
// b2 val: b1
// b2 timer start: 1000
// c1 timer end  : 1000
// c2 val: c1
// c2 timer start: 1000
// a2 timer end  : 1000
// b2 timer end  : 1000
// b3 val: b2
// c2 timer end  : 1000
// c3 val: c2
// c3 timer start: 1000
// c3 timer end  : 1000

USAGE:

PromiseThunk Specification

new PromiseThunk(setup)

how to make promise.

p = new PromiseThunk(
  function setup(resolve, reject) {
    // async process -> resolve(value) or reject(error)
    try { resolve('value'); }
    catch (error) { reject(error); }
  }
);
// setup(
//  function resolve(value) {},
//  function reject(error) {})

example

var p = new PromiseThunk(
  function setup(resolve, reject) {
    setTimeout(function () {
      if (Math.random() < 0.5) resolve('value');
      else reject(new Error('error'));
    }, 100);
  }
);

// promise
p.then(
  function (val) { console.info('val:', val); },
  function (err) { console.error('err:', err); });

// thunk
p(function (err, val) { console.info('val:', val, 'err:', err); });

// mixed chanable
p(function (err, val) { console.info('val:', val, 'err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
.then(
  function (val) { console.info('val:', val); },
  function (err) { console.error('err:', err); })
.then(
  function (val) { console.info('val:', val); },
  function (err) { console.error('err:', err); })
(function (err, val) { console.info('val:', val, 'err:', err); })
.then(
  function (val) { console.info('val:', val); },
  function (err) { console.error('err:', err); });

PromiseThunk.convert(p) -> promise thunk

convert standard promise to promise thunk.

PromiseThunk.wrap(fn) -> function returns promise thunk

also thenable, yieldable, callable. same as thunkify.

PromiseThunk.thunkify(fn) -> function returns promise thunk

also thenable, yieldable, callable. same as wrap.

promise.then(onFulfilled, onRejected)

how to use promise.

p = p.then(
  function resolved(value) {},
  function rejected(error) {});

example

p = p.then(
  function resolved(value) {
    console.info(value);
  },
  function rejected(error) {
    console.error(error);
  });

promise.catch(onRejected)

how to catch error from promise.

p = p.catch(
  function rejected(error) {});

or

when you use old browser

p = p['catch'](
  function rejected(error) {});

PromiseThunk.all(iterable or array)

wait for all promises.

p = PromiseThunk.all([promise, ...]);

PromiseThunk.race(iterable or array)

get value or error of first finished promise.

p = PromiseThunk.race([promise, ...]);

PromiseThunk.resolve(value or promise)

get resolved promise.

p = PromiseThunk.resolve(value or promise);

PromiseThunk.reject(error)

get rejected promise.

p = PromiseThunk.reject(error);

PromiseThunk.accept(value)

get resolved (accepted) promise.

p = PromiseThunk.accept(value);

PromiseThunk.defer()

make deferred object with promise.

dfd = PromiseThunk.defer();
// -> {promise, resolve, reject}

SEE ALSO:

npm promise-light

npm aa

npm co

LICENSE:

MIT

Keywords

FAQs

Package last updated on 18 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc