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

promise-wtf

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-wtf

Lightweight Promise implementation with the 'finally' method

  • 0.1.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-4.05%
Maintainers
1
Weekly downloads
 
Created
Source

promise-wtf

Lightweight Promise implementation with the "finally" method

NPM Travis Coverage Status devDependency Status

Why

Native Promise in ECMAScript 2015 came without "finally" while this method is really useful in many cases.

For an instance, let's start with the following script:

var Article = require('../models/Article');

export var home = (req, res) => {

  let query = req.query || {};
  let skip = query.skip || 0;
  let limit = query.limit || 10;

  let data = {
    error: 0,
    entries: []
  };

I don't think that's good to write something like this:

  return Article.list(skip, limit).then((result) => {
    data.entries = result;
    res.render('landing', data);
  }).catch((err) => {
    data.error = err;
    res.render('landing', data);
  });
};

However, it's better to have "finally" there:

  return Article.list(skip, limit).then((result) => {
    data.entries = result;
  }).catch((err) => {
    data.error = err;
  }).finally(() => {
    res.render('landing', data);
  });
};

Unfortunately, "finally" is only available in some libraries such as Bluebird, or Q+, those are quite heavy to load for client side usage. What I need is just a basic prototype, a simple polyfill with "finally" implemented.

What

This version only supports the Promise constructor and 4 static methods:

How

In node.js

npm install promise-wtf

And then:

var fs = require('fs');
var Promise = require('promise-wtf');

var read = (file) => {
  return new Promise((resolve, reject) => {
    fs.readFile(file, 'utf8', (err, content) => {
      if (err) {
        return reject(err);
      }
      return resolve(content);
    });
  });
};

read('./hello.txt').then((a) => {
  a += 'I like Promise';
  return a;
}).then((b) => {
  b += '\nI need finally';
  return b;
}).then((c) => {
  console.log(c);
}).catch((err) => {
  console.log(err);
}).finally(() => {
  console.log('Done');
});

Ouput:

Hello
I like Promise
I need finally
Done
Using SystemJS
  System.config({
    baseURL: '/path/to/promise-wtf.js/folder',
    map: {
      promise: 'promise-wtf'
    }
  });

  System.import('promise').then(function(Promise){
    // use Promise here
  });

Using RequireJS
require.config({
  baseUrl: '/path/to/promise-wtf.js/folder',
  paths: {
    promise: 'promise-wtf'
  }
});

requirejs('promise', function(Promise){
  // use Promise here
});

CDN

Promise.min.js

Test

git clone https://github.com/ndaidong/promise-wtf.git
cd promise-wtf
npm install
npm test

License

The MIT License (MIT)

Keywords

FAQs

Package last updated on 13 Jun 2016

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