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 1303240 80976 ?       Ssl  Oct24 226:02 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa        1200  0.9  0.8 11724624 139260 ?     Ssl  Oct24 398:01 node /home/sasa/dex8/dex8-api/starter.js
sasa        1207  0.4  0.4 1305712 74256 ?       Ssl  Oct24 181:37 node /home/sasa/regoch/regoch-www/server
sasa        1208  0.3  0.4 1304656 72444 ?       Ssl  Oct24 161:26 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1224  0.9  0.5 11801636 93344 ?      Ssl  Oct24 394:46 node /home/sasa/dex8/dex8-www/server/index.js
sasa        1236  0.5  0.6 11813644 99088 ?      Ssl  Oct24 246:11 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1255  0.7  0.5 1330064 83164 ?       Ssl  Oct24 322:24 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa        1257  0.3  0.4 1301160 70148 ?       Ssl  Oct24 163:58 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.3  0.4 1297336 67716 ?       Ssl  Oct24 161:50 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  3.2  2.8 12174452 470248 ?     Ssl  Oct24 1353:24 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1449  0.4  0.3 1166312 64956 ?       Ssl  Oct24 169:37 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1528  1.8  2.6 22639432 434924 ?     Ssl  Oct24 762:32 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1529  0.4  0.3 1099876 63880 ?       Ssl  Oct24 169:32 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1606  0.4  0.3 1099620 63836 ?       Ssl  Oct24 169:35 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1607  0.4  0.3 1100132 63092 ?       Ssl  Oct24 169:05 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1705  0.4  0.3 1100648 64936 ?       Ssl  Oct24 168:49 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1706  0.4  0.3 1100388 64228 ?       Ssl  Oct24 169:02 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1805  0.4  0.3 1100384 64092 ?       Ssl  Oct24 169:02 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1807  0.3  0.3 1100384 64052 ?       Ssl  Oct24 165:36 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1885  0.3  0.3 1099880 63640 ?       Ssl  Oct24 166:39 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1886  1.5  0.9 11853812 155676 ?     Ssl  Oct24 641:31 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
www-data 2270090  0.0  0.0   2608   592 ?        S    13:28   0:00 sh -c ps -aux | grep node
www-data 2270092  0.0  0.0   3304   716 ?        S    13:28   0:00 grep node
sasa     2628167  0.8  0.7 1165396 121384 ?      Ssl  Nov04 225:23 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     3168557  0.6  0.5 11820472 94784 ?      Ssl  Nov06 151:54 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.4  0.5 11798408 83024 ?      Ssl  Nov06 100:44 node /home/sasa/@mikosoft/dodo-examples/server/index.js