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.5 1304016 83148 ?       Ssl   2025 614:38 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa        1208  0.3  0.4 1305000 73936 ?       Ssl   2025 445:30 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1224  0.9  0.6 11825800 113900 ?     Ssl   2025 1027:18 node /home/sasa/dex8/dex8-www/server/index.js
sasa        1236  0.6  0.6 11819212 103448 ?     Ssl   2025 702:41 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1255  0.8  0.5 1331116 84412 ?       Ssl   2025 962:04 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa        1257  0.4  0.4 1303640 71392 ?       Ssl   2025 453:23 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.3  0.4 1298252 67516 ?       Ssl   2025 444:49 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  4.1  3.0 12181628 494204 ?     Ssl   2025 4693:35 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1886  2.7  1.2 11894996 200984 ?     Ssl   2025 3028:25 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
sasa     1214480  0.3  0.3 1100424 65316 ?       Ssl   2025  62:29 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214483  0.3  0.3 1100288 65304 ?       Ssl   2025  62:39 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214536  0.3  0.4 1100548 66212 ?       Ssl   2025  62:21 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214544  0.3  0.4 1100776 65848 ?       Ssl   2025  62:53 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214588  0.3  0.4 1100804 65952 ?       Ssl   2025  62:28 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214617  0.3  0.3 1100416 65448 ?       Ssl   2025  62:18 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214641  0.3  0.3 1099368 64320 ?       Ssl   2025  61:22 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214671  0.3  0.3 1099104 62764 ?       Ssl   2025  61:11 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1215731  0.5  0.7 11703676 118096 ?     Ssl   2025 100:10 node /home/sasa/dex8/dex8-api/starter.js
sasa     1216489  0.3  0.3 1100024 65156 ?       Ssl   2025  62:08 node /home/sasa/dex8/dex8-worker/starter.js
sasa     2546804  0.3  0.4 1102180 67012 ?       Ssl  03:03   1:52 node /home/sasa/dex8/dex8-worker/starter.js
www-data 2585003  0.0  0.0   2608   596 ?        S    11:41   0:00 sh -c ps -aux | grep node
www-data 2585005  0.0  0.0   3304   720 ?        S    11:41   0:00 grep node
sasa     2628167  0.9  0.8 1177628 131464 ?      Ssl   2025 916:25 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     3168557  0.7  0.5 11813680 96212 ?      Ssl   2025 659:07 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.5  0.5 11798920 84488 ?      Ssl   2025 483:55 node /home/sasa/@mikosoft/dodo-examples/server/index.js