Exemplo n.º 1
0
(function () {
  'use strict';

  var grunt = require('grunt');

  // For the sake of this demo, the bluebird NPM package must explicitly use
  // "require" here so Tonic will parse and load the module. In your own code,
  // you can reduce this to one line and have the plugin automatically require
  // an NPM Promise module for you:
  //
  // var Promise = require('grunt-promise').using('bluebird');

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

  // Register a promised task (working example).
  grunt.registerPromise('timeout', function () {
    return new Promise(function (resolve) {
      setTimeout(function () {
        resolve('Hello World!');
      }, 1000); // 1 second
    }).then(console.log);
  });

  // Manually run grunt task.
  // DO NOT USE: This is only to get this example in tonic working.
  grunt.task.run('timeout');
  grunt.task.start();

})();
Exemplo n.º 2
0
(function () {
  'use strict';

  var grunt = require('grunt');
  var Promise = require('./index.js').using();

  // Grunt config initialization.
  // -----------------------------------------------------------------------
  grunt.initConfig({
    bump: {
      options: {
        pushTo: 'origin'
      }
    },
    nodeunit: {
      all: ['test/*-test.js']
    },
    eslint: {
      options: {
        configFile: '.eslintrc'
      },
      js: [
        'index.js',
        'Gruntfile.js',
        'lib/**/*.js',
        'script/**/*.js',
        'test/**/*.js'
      ]
    }
  });

  // Load NPM grunt module tasks.
  // -----------------------------------------------------------------------
  grunt.loadNpmTasks('grunt-bump');
  grunt.loadNpmTasks('grunt-eslint');
  grunt.loadNpmTasks('grunt-contrib-nodeunit');

  // Register regular Grunt tasks.
  // -----------------------------------------------------------------------
  grunt.registerTask('default', ['test']);
  grunt.registerTask('test', ['eslint', 'nodeunit']);
  grunt.registerTask('test-before', grunt.log.writeln.bind(grunt.log, 'before finished'));
  grunt.registerTask('test-after', grunt.log.writeln.bind(grunt.log, 'after finished'));

  // Register Promised based Grunt task.
  grunt.registerPromise('test-chaining', function () {
    var math = require('./test/fixtures/math');
    return math.add()
      .then(math.multiply)           // Expected value: 50
      .then(math.multiply)           // Expected value: 500
      .then(math.multiply)           // Expected value: 5000
      .then(math.multiply)           // Expected value: 50000
      .then(function (value) {
        var result = 'Result (' + Promise.gruntPromiseLibraryName + '): ' + value;
        grunt.log.writeln(result);   // Result: 50000
      });
  });

})();