Test JS script file

Selected file: 50bluebird_newPromise.js
Compare console.log() outputs in browser and NodeJS.
Notice: Open browser's console in FireBUG!

  1. 01callback.js
  2. 02callback_settimeout.js
  3. 02callback_settimeout2.js
  4. 50bluebird_basic.js
  5. 50bluebird_newPromise.js
  6. 51bluebird_fs.js

..:: Source code - 50bluebird_newPromise.js ::..

/**
 * basic usage of bluebird
 */

var Promise = require("bluebird");

//defining an async function
var clAsync = function () {
  var P = new Promise(function (resolve, reject) {
    console.log('Write from function');
    resolve('ok');
    reject('not ok');
  });

  /*
  {
  "isFulfilled": true,
  "isRejected": false,
  "fulfillmentValue": "ok"
  }
   */
  console.log(JSON.stringify(P, null, 2));

  return P;
};

console.log(JSON.stringify(clAsync, null, 2)); //undefined

//executing promisified function clAsync() and adding new callback with then method
clAsync()

  .then(function (returnedVal) {
    console.log('THEN 1 - fulfillmentValue= ' + returnedVal);
    return {one: 1, two: 'drugi'}; //returning to next then
  })

  .then(function (returnedVal) {
    console.logs('THEN 2 - fulfillmentValue= ' + returnedVal.one + returnedVal.two);
    // return new Error('Namjerna greška');  --doesn't work
    throw new Error('Namjerna greška', 'someFile.js', 152);
  })

  //catch type errors
  .catch(TypeError, function (e) {
    console.error(e.stack);
    // console.error(e.message);
  })

  //catch all other errors
  .catch(function (e) {
    console.error(e.message);
    // console.error(e.stack);
  });
 

..:: NodeJS Console - 50bluebird_newPromise.js ::..

$ node 50bluebird_newPromise.js

		
		
$ps -aux | grep node (list all node processes)
sasa         831  0.4  0.4 1301596 73096 ?       Ssl  Jun15   8:05 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa         843  0.4  0.6 11696520 100996 ?     Ssl  Jun15   7:24 node /home/sasa/dex8/dex8-api/starter.js
sasa         858  0.3  0.3 1033488 61844 ?       Ssl  Jun15   5:53 node /home/sasa/regoch/regoch-www/server
sasa         867  0.3  0.3 1292724 58688 ?       Ssl  Jun15   5:45 node /home/sasa/dex8/dex8-panel/server/index.js
sasa         886  0.8  0.5 11811900 94380 ?      Ssl  Jun15  13:28 node /home/sasa/dex8/dex8-www/server/index.js
sasa         888  0.3  0.4 11790104 74052 ?      Ssl  Jun15   6:12 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa         904  0.6  0.4 1329612 81872 ?       Ssl  Jun15  10:32 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa         910  0.3  0.3 1295860 63976 ?       Ssl  Jun15   5:47 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa         969  0.3  0.3 1160132 56276 ?       Ssl  Jun15   5:41 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa         970  0.4  0.5 11798320 86068 ?      Ssl  Jun15   8:07 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa        1010  0.4  0.5 1138968 92232 ?       Ssl  Jun15   7:25 node /home/sasa/voovuu/voovuu-api/starter.js
sasa        1016  0.6  0.6 11813896 106840 ?     Ssl  Jun15  10:32 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1086  0.3  0.4 11662304 75548 ?      Ssl  Jun15   6:06 node /home/sasa/@mikosoft/dodo-examples/server/index.js
sasa        1089  0.3  0.4 1100636 65628 ?       Ssl  Jun15   5:58 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1159  0.3  0.4 1100640 66012 ?       Ssl  Jun15   5:56 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1160  0.3  0.3 1100636 65272 ?       Ssl  Jun15   5:56 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1241  0.3  0.3 1101976 63760 ?       Ssl  Jun15   5:54 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1242  0.3  0.4 22279100 78292 ?      Ssl  Jun15   6:11 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1323  0.3  0.3 1100636 64732 ?       Ssl  Jun15   5:55 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1324  0.3  0.3 1100640 65036 ?       Ssl  Jun15   5:55 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1398  0.3  0.3 1100640 65508 ?       Ssl  Jun15   5:54 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1400  0.3  0.3 1100636 65248 ?       Ssl  Jun15   5:56 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1465  0.3  0.3 1100640 65132 ?       Ssl  Jun15   5:53 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1475  0.3  0.6 11811108 106624 ?     Ssl  Jun15   6:32 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
www-data   83489  0.0  0.0   2608   608 ?        S    06:46   0:00 sh -c ps -aux | grep node
www-data   83491  0.0  0.0   3304   720 ?        S    06:46   0:00 grep node