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         914  0.5  0.5 1306752 85468 ?       Ssl  Aug04 294:41 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa         935  0.7  0.8 11786840 132632 ?     Ssl  Aug04 437:47 node /home/sasa/dex8/dex8-api/starter.js
sasa         938  0.4  0.4 1306588 75504 ?       Ssl  Aug04 246:47 node /home/sasa/regoch/regoch-www/server
sasa         945  0.3  0.4 1303752 71444 ?       Ssl  Aug04 213:02 node /home/sasa/dex8/dex8-panel/server/index.js
sasa         947  0.7  0.6 11821496 99368 ?      Ssl  Aug04 435:16 node /home/sasa/dex8/dex8-www/server/index.js
sasa         966  0.5  0.5 11807952 96508 ?      Ssl  Aug04 326:52 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa         980  0.7  0.5 1330216 84316 ?       Ssl  Aug04 443:33 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa         997  0.3  0.4 1303608 72484 ?       Ssl  Aug04 215:19 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1021  0.3  0.3 1297448 65084 ?       Ssl  Aug04 212:12 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1065  0.6  0.5 11801500 92852 ?      Ssl  Aug04 360:47 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa        1074  0.5  0.6 1150720 103408 ?      Ssl  Aug04 351:47 node /home/sasa/voovuu/voovuu-api/starter.js
sasa        1136  0.6  0.9 11850660 151756 ?     Ssl  Aug04 409:43 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1148  0.3  0.4 11794644 78292 ?      Ssl  Aug04 234:42 node /home/sasa/@mikosoft/dodo-examples/server/index.js
sasa        1197  0.3  0.3 1099884 63672 ?       Ssl  Aug04 219:26 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1199  0.3  0.4 22279548 79200 ?      Ssl  Aug04 222:19 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1308  0.3  0.3 1099872 63428 ?       Ssl  Aug04 218:45 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1325  0.3  0.3 1099360 63260 ?       Ssl  Aug04 218:36 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1393  0.7  0.8 22327868 145504 ?     Ssl  Aug04 456:23 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1395  0.3  0.3 1099884 63448 ?       Ssl  Aug04 219:41 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1489  0.3  0.3 1100384 64068 ?       Ssl  Aug04 219:30 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1496  0.3  0.3 1100640 65164 ?       Ssl  Aug04 218:23 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1607  0.3  0.3 1099872 63336 ?       Ssl  Aug04 218:51 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1608  0.3  0.3 1099872 62952 ?       Ssl  Aug04 217:43 node /home/sasa/dex8/dex8-worker/starter.js
sasa        1686  1.0  0.9 11849024 150860 ?     Ssl  Aug04 618:42 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
www-data  475307  0.0  0.0   2608   596 ?        S    12:52   0:00 sh -c ps -aux | grep node
www-data  475309  0.0  0.0   3304   720 ?        S    12:52   0:00 grep node
sasa     1371685  0.4  0.5 11797416 83468 ?      Ssl  Aug16 172:03 node /home/sasa/mikosoft-info/solar-wind-energy/server/index.js
sasa     1460918  0.4  0.3 1300136 65296 ?       Ssl  Aug17 169:32 node /home/sasa/sites-nodejs/oxygen-free-copper/server.js