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        1208  0.3  0.4 1309696 75212 ?       Ssl   2025 1556:12 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1236  0.5  0.6 11823300 101008 ?     Ssl   2025 2457:18 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1257  0.3  0.4 1308260 74412 ?       Ssl   2025 1575:13 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.3  0.4 1301520 68804 ?       Ssl   2025 1551:44 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  4.2  4.0 12358032 658108 ?     Ssl   2025 19945:29 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1886  7.5  3.7 12280452 614608 ?     Ssl   2025 35077:02 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
www-data  887493  0.0  0.0   2608   600 ?        S    17:28   0:00 sh -c ps -aux | grep node
www-data  887495  0.0  0.0   3304   708 ?        S    17:28   0:00 grep node
sasa     1214480  0.3  0.7 22311168 123892 ?     Ssl   2025 1354:38 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214483  0.3  0.3 1101824 64244 ?       Ssl   2025 1243:24 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214536  0.3  0.3 1100804 64648 ?       Ssl   2025 1241:45 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214544  0.3  0.4 1102312 65732 ?       Ssl   2025 1251:57 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214588  0.3  0.3 1102340 64856 ?       Ssl   2025 1245:31 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214617  0.3  0.4 1102208 66280 ?       Ssl   2025 1241:09 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214641  0.3  0.3 1166184 64080 ?       Ssl   2025 1232:38 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214671  0.3  0.3 1100128 63052 ?       Ssl   2025 1226:41 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1215731  0.8  0.7 11847492 124064 ?     Ssl   2025 3318:08 node /home/sasa/dex8/dex8-api/starter.js
sasa     1216489  0.3  0.3 1101304 64556 ?       Ssl   2025 1241:41 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1399960  0.8  0.7 11822880 117828 ?     Ssl  Apr20 1839:50 node /home/sasa/dex8/dex8-www/server/index.js
sasa     1631573  0.4  0.5 1304256 82540 ?       Ssl  May31 756:09 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa     1824679  0.4  0.5 11798848 83568 ?      Ssl  Aug15 191:03 node /home/sasa/kriptus/kriptus-panel/server/index.js
sasa     2448036  1.2  0.9 11856820 159600 ?     Ssl  Aug21 418:30 node /home/sasa/miko/miko-hr/server/index.js
sasa     2628167  0.7  0.7 1176796 130712 ?      Ssl   2025 3564:15 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     2663246  2.4  0.7 11847724 129300 ?     Ssl  Aug23 754:22 node /home/sasa/kriptus/kriptus-api/starter.js
sasa     3168557  0.6  0.5 11806044 94788 ?      Ssl   2025 2753:03 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.5  0.5 11799488 83464 ?      Ssl   2025 2266:22 node /home/sasa/@mikosoft/dodo-examples/server/index.js
sasa     3264954  0.6  0.5 1333120 86476 ?       Ssl  Feb21 2039:50 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa     3358795  0.6  0.5 1266904 88868 ?       Ssl  Aug29 137:08 node /home/sasa/miko/miko-hr/api/starter.js
sasa     3809381  0.3  0.5 11804708 94392 ?      Ssl  Sep02  56:14 node /home/sasa/dex8/dex8-worker/starter.js