npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-promise

v1.1.3

Published

Create asynchronous Grunt tasks using Promises.

Downloads

207

Readme

grunt-promise Build Status

Join the chat at https://gitter.im/unicorn-fail/grunt-promise

Create asynchronous Grunt tasks using Promises.

Getting Started

This package requires Node.js ^0.12. This package requires Grunt ^0.4.5.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

Install

npm install grunt-promise --save-dev

Supported NPM Promise Packages

Below are a list of supported NPM modules that implement Promise APIs. The goal of this plugin is to abstract the Grunt integration so it does not limit or assume how one creates or consumes the Promises in a Grunt task; it merely facilitates communication.

By default, this plugin will attempt to automatically detect if one of the NPM modules below is already installed (order of appearance). If no NPM module is detected, it defaults to using the native Promise object.

Note: This plugin does NOT depend on any Promise based NPM modules.

You, of course, are not limited in using any of these modules, they are simply supported "out-of-the-box". For customization and other Promise solutions, see below.

Basic Usage

Require the plugin's NPM module at the top and call the using method with the name of the Promise module you wish to use. If no module is specified, it will attempt to load the first available Promise object in environment. You can use this Promise object to create new Promises, see example below.

// Gruntfile.js
var Promise = require('grunt-promise').using('bluebird');

module.exports = function (grunt) {
  // Continue your grunt initialization as normal.
  grunt.initConfig({...});
}

Highly Recommended - Always specify an NPM module

It is highly recommended that you are always explicit when loading a Promise object via this plugin to avoid confusion and any randomness if/when other NPM modules may depend on a Promise library different from what you are expecting.

There are several ways to specify which Promise object will be returned from this plugin:

  1. Pass a NPM package name when loading the Promise object. This can either be a string or a function that returns a string. It will automatically require the module if not already loaded:

    var Promise = require('grunt-promise').using('an-npm-promise-module');

    Or:

    var Promise = require('grunt-promise').using(function () {
      return 'an-npm-promise-module';
    });
  2. Pass an existing module (or create a new one). This must be a function that either creates a "thenable" Promise object or a plain object that has exported this constructor to a "Promise" method (e.g. module.exports.Promise):

    var promiseModule = require('an-npm-promise-module');
    var Promise = require('grunt-promise').using(promiseModule);

    Or:

    var Promise = require('grunt-promise').using(function () {
      return require('an-npm-promise-module');
    });
  3. Grunt Option - The following option is available where some use cases may require a little bit of CLI love. In which case, you can use:

    grunt --grunt-promise-library=<module>

    Where <module> is an NPM package module. You may also specify native here.

Available Grunt Methods

This plugin provides two new methods on the main grunt object that help save a little bit of time and whitespace. It's worth noting that you do not have to call this.async() inside these tasks (which is the whole point for this plugin).

  • grunt.registerPromise(name, info, fn) - Normal task
  • grunt.registerMultiPromise(name, info, fn) - Multi task

Arguments:

  • {string} name - The name of the Grunt task to register.
  • {string|function} info - (Optional) Descriptive text explaining what the task does. Shows up on --help. You may omit this argument and replace it with fn instead.
  • {function} fn - (Required) The task function. Remember not to pass in your Promise function directly. Promise resolvers are immediately invoked when they are created. You must wrap the Promise with an anonymous task function instead. The task function must return a Promise or it will fail.

Examples:

var grunt = require('grunt');
var Promise = require('grunt-promise').using('bluebird');

// Register a promised task (working example).
// @see https://tonicdev.com/npm/grunt-promise
grunt.registerPromise('timeout', function () {
  return new Promise(function (resolve) {
    setTimeout(function () {
      resolve('Hello World!');
    }, 1000); // 1 second.
  }).then(grunt.log.write);
});

// Register a promised task (workflow example).
grunt.registerPromise('my-promise', function () {
  return promiseReturningFunction()
    .then(anotherPromise)
    .then(yetAnotherPromise)
    .then(function (value) {
      grunt.log.writeln('Value:', value);
    })
    .catch(function (e) {
       // Do something with your errors.
       // It's not entirely necessary to implement this. This plugin
       // already appends the necessary catch/then handlers to properly
       // fail or end the async task.
       grunt.fail.fatal(e);
    });
});

Creator

Mark Halliwell

Copyright and license

Code and documentation Copyright 2016 Mark Halliwell. Released under the MIT license.