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        1194  0.5  0.4 1303108 81788 ?       Ssl  Oct24 455:27 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa        1200  1.0  0.8 11725056 142312 ?     Ssl  Oct24 821:00 node /home/sasa/dex8/dex8-api/starter.js
sasa        1208  0.4  0.4 1305000 73744 ?       Ssl  Oct24 328:24 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1224  1.0  0.7 11834016 116104 ?     Ssl  Oct24 781:08 node /home/sasa/dex8/dex8-www/server/index.js
sasa        1236  0.6  0.6 11813644 98864 ?      Ssl  Oct24 516:54 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1255  0.9  0.5 1330860 84832 ?       Ssl  Oct24 698:36 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa        1257  0.4  0.4 1300548 68744 ?       Ssl  Oct24 334:02 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.4  0.4 1297772 66272 ?       Ssl  Oct24 328:18 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  3.8  2.7 12141948 446992 ?     Ssl  Oct24 2974:31 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1449  0.4  0.3 1232104 64808 ?       Ssl  Oct24 345:15 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1528  2.0  2.6 33091860 434520 ?     Ssl  Oct24 1558:59 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1529  0.4  0.4 1102436 65748 ?       Ssl  Oct24 344:24 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1606  0.4  0.4 1102436 66612 ?       Ssl  Oct24 345:32 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1607  0.4  0.4 1102692 65976 ?       Ssl  Oct24 344:27 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1705  0.4  0.4 1102696 67092 ?       Ssl  Oct24 344:05 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1706  0.4  0.3 1101412 64748 ?       Ssl  Oct24 343:29 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1805  0.4  0.4 1168992 67640 ?       Ssl  Oct24 344:32 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1807  0.4  0.4 1102944 66844 ?       Ssl  Oct24 336:29 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1885  0.4  0.4 1102184 66396 ?       Ssl  Oct24 338:12 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1886  2.4  1.0 11867400 170276 ?     Ssl  Oct24 1870:34 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
sasa     2628167  1.0  0.8 1175384 131288 ?      Ssl  Nov04 622:58 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     3168557  0.7  0.5 11812600 96456 ?      Ssl  Nov06 454:46 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.5  0.5 11798920 83364 ?      Ssl  Nov06 308:18 node /home/sasa/@mikosoft/dodo-examples/server/index.js
www-data 3991658  0.0  0.0   2608   528 ?        S    01:27   0:00 sh -c ps -aux | grep node
www-data 3991660  0.0  0.0   3304   648 ?        S    01:27   0:00 grep node