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.5  0.4 1303692 78664 ?       Ssl  Jun15 224:52 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa         843  0.5  0.7 11711456 123608 ?     Ssl  Jun15 251:55 node /home/sasa/dex8/dex8-api/starter.js
sasa         858  0.3  0.4 1306304 74440 ?       Ssl  Jun15 176:21 node /home/sasa/regoch/regoch-www/server
sasa         867  0.3  0.4 1301192 69972 ?       Ssl  Jun15 161:57 node /home/sasa/dex8/dex8-panel/server/index.js
sasa         886  0.7  0.5 11802388 94584 ?      Ssl  Jun15 350:51 node /home/sasa/dex8/dex8-www/server/index.js
sasa         888  0.5  0.4 11792500 78004 ?      Ssl  Jun15 239:58 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa         904  0.7  0.5 1329652 82100 ?       Ssl  Jun15 326:36 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa         910  0.3  0.4 1298960 67696 ?       Ssl  Jun15 162:59 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa         969  0.3  0.4 1298376 65852 ?       Ssl  Jun15 161:03 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa         970  0.6  0.5 11812676 95604 ?      Ssl  Jun15 291:07 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa        1010  0.6  0.6 1147588 102492 ?      Ssl  Jun15 272:32 node /home/sasa/voovuu/voovuu-api/starter.js
sasa        1016  0.7  1.1 11909044 196056 ?     Ssl  Jun15 341:37 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1086  0.3  0.4 11794656 78076 ?      Ssl  Jun15 177:14 node /home/sasa/@mikosoft/dodo-examples/server/index.js
sasa        1089  0.3  0.3 1100124 63756 ?       Ssl  Jun15 165:40 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1159  0.3  0.4 22277692 78796 ?      Ssl  Jun15 170:01 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1160  0.3  0.3 1099100 62876 ?       Ssl  Jun15 165:54 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1241  0.3  0.3 1101208 62968 ?       Ssl  Jun15 166:25 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1242  0.4  0.7 22311220 123200 ?     Ssl  Jun15 202:11 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1323  0.3  0.3 1099612 63228 ?       Ssl  Jun15 166:03 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1324  0.3  0.3 1100128 63708 ?       Ssl  Jun15 165:59 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1398  0.3  0.3 1099104 62748 ?       Ssl  Jun15 165:59 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1400  0.3  0.3 1100380 64016 ?       Ssl  Jun15 165:55 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1465  0.3  0.3 1099872 63292 ?       Ssl  Jun15 165:15 node /home/sasa/dex8/dex8-worker/starter.js
sasa      178968  0.8  0.7 11821340 119744 ?     Ssl  Jun17 353:35 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
sasa     1802958  0.7  0.5 1317300 92004 ?       Ssl  Jul09  70:36 node /home/sasa/crypto/dex-arbitrage-bot/scripts/observer/index-
www-data 2393863  0.0  0.0   2608   600 ?        S    22:02   0:00 sh -c ps -aux | grep node
www-data 2393865  0.0  0.0   3304   728 ?        S    22:02   0:00 grep node