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 1303428 82344 ?       Ssl  Oct24 158:04 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa        1200  1.0  0.8 11722680 138656 ?     Ssl  Oct24 278:10 node /home/sasa/dex8/dex8-api/starter.js
sasa        1207  0.4  0.4 1304652 72436 ?       Ssl  Oct24 123:10 node /home/sasa/regoch/regoch-www/server
sasa        1208  0.4  0.4 1303888 70272 ?       Ssl  Oct24 112:58 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1224  1.0  0.5 11805868 94404 ?      Ssl  Oct24 285:13 node /home/sasa/dex8/dex8-www/server/index.js
sasa        1236  0.6  0.6 11813132 98368 ?      Ssl  Oct24 169:44 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1255  0.8  0.5 1329104 82144 ?       Ssl  Oct24 219:53 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa        1257  0.4  0.4 1300740 69568 ?       Ssl  Oct24 114:35 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.4  0.3 1297148 65464 ?       Ssl  Oct24 113:10 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  2.9  2.6 12140180 441384 ?     Ssl  Oct24 791:32 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1449  0.4  0.3 1165544 63620 ?       Ssl  Oct24 118:50 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1528  1.9  2.6 22614656 432648 ?     Ssl  Oct24 523:34 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1529  0.4  0.3 1101412 65420 ?       Ssl  Oct24 118:42 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1606  0.4  0.4 1101412 65628 ?       Ssl  Oct24 118:50 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1607  0.4  0.4 1101412 65628 ?       Ssl  Oct24 118:25 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1705  0.4  0.4 1101672 66696 ?       Ssl  Oct24 118:07 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1706  0.4  0.4 1101668 66488 ?       Ssl  Oct24 118:21 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1805  0.4  0.4 1101408 66472 ?       Ssl  Oct24 118:19 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1807  0.4  0.3 1100128 63992 ?       Ssl  Oct24 115:54 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1885  0.4  0.3 1099112 63996 ?       Ssl  Oct24 116:18 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1886  1.4  0.8 11836556 136124 ?     Ssl  Oct24 382:05 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
sasa     2628167  1.0  0.6 1158560 111492 ?      Ssl  Nov04 110:46 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     3168557  0.7  0.6 11826032 99808 ?      Ssl  Nov06  65:07 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.5  0.5 11798152 83368 ?      Ssl  Nov06  45:40 node /home/sasa/@mikosoft/dodo-examples/server/index.js
www-data 4143131  0.0  0.0   2608   592 ?        S    08:58   0:00 sh -c ps -aux | grep node
www-data 4143133  0.0  0.0   3304   716 ?        S    08:58   0:00 grep node