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.2 1310368 47992 ?       Ssl   2025 1690:57 node /home/sasa/dex8/dex8-panel/server/index.js
sasa        1236  0.5  0.4 11823420 74268 ?      Ssl   2025 2670:11 node /home/sasa/mikosoft_hr/www-mikosoft-hr/server/index.js
sasa        1257  0.3  0.2 1305752 44988 ?       Ssl   2025 1711:45 node /home/sasa/mikosoft-info/www-mikosoft-info/server/index.js
sasa        1289  0.3  0.2 1301856 42580 ?       Ssl   2025 1685:36 node /home/sasa/dex8/dex8-kiosk/server/index.js
sasa        1363  4.2  3.1 12262868 522020 ?     Ssl   2025 21639:43 node /home/sasa/voovuu/voovuu-pub/server/index.js
sasa        1886  8.1  4.2 12377424 696280 ?     Ssl   2025 41726:17 node /home/sasa/mikosoft-info/crypto-mikosoft-info/server/index.
www-data   63016  0.0  0.0   2608   600 ?        S    17:51   0:00 sh -c ps -aux | grep node
www-data   63018  0.0  0.0   3304   644 ?        S    17:51   0:00 grep node
sasa     1214480  0.3  0.5 22311168 91364 ?      Ssl   2025 1503:22 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214483  0.3  0.2 1101568 34308 ?       Ssl   2025 1385:59 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214536  0.3  0.2 1100804 34528 ?       Ssl   2025 1384:03 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214544  0.3  0.2 1102312 35440 ?       Ssl   2025 1395:38 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214588  0.3  0.2 1102084 34592 ?       Ssl   2025 1388:40 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214617  0.3  0.2 1101952 35076 ?       Ssl   2025 1383:39 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214641  0.3  0.2 1165928 33628 ?       Ssl   2025 1375:13 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1214671  0.3  0.2 1101664 34132 ?       Ssl   2025 1368:31 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1216489  0.3  0.2 1101560 34236 ?       Ssl   2025 1384:32 node /home/sasa/dex8/dex8-worker/starter.js
sasa     1399960  0.8  0.5 11817308 87168 ?      Ssl  Apr20 2208:32 node /home/sasa/dex8/dex8-www/server/index.js
sasa     1631573  0.6  0.3 1303652 57120 ?       Ssl  May31 1227:28 node /home/sasa/.pm2/modules/pm2-logrotate/node_modules/pm2-logr
sasa     1824679  0.4  0.3 11798848 55720 ?      Ssl  Aug15 393:53 node /home/sasa/kriptus/kriptus-panel/server/index.js
sasa     2072475  0.7  0.5 1197660 95560 ?       Ssl  Sep24 201:32 node /home/sasa/voovuu/voovuu-api/starter.js
sasa     2075109  8.6  0.5 11712096 93012 ?      Ssl  Sep24 2356:42 node /home/sasa/dex8/dex8-api/starter.js
sasa     2075416  0.6  0.3 1332048 58644 ?       Ssl  Sep24 178:34 node /home/sasa/mikosoft-info/api-mikosoft-info/starter.js
sasa     2448036  1.1  0.8 11857104 131896 ?     Ssl  Aug21 907:59 node /home/sasa/miko/miko-hr/server/index.js
sasa     2663246  2.2  0.6 11844252 99176 ?      Ssl  Aug23 1630:50 node /home/sasa/kriptus/kriptus-api/starter.js
sasa     3168557  0.6  0.4 11812308 72304 ?      Ssl   2025 3028:44 node /home/sasa/mikosoft-info/dodo-mikosoft-info/server/index.js
sasa     3169051  0.5  0.3 11799488 54620 ?      Ssl   2025 2484:00 node /home/sasa/@mikosoft/dodo-examples/server/index.js
sasa     3615181  0.6  0.3 1331896 63420 ?       Ssl  Oct07  53:57 node /home/sasa/miko/miko-hr/api/starter.js
sasa     3809381  0.3  0.4 22302664 79244 ?      Ssl  Sep02 211:47 node /home/sasa/dex8/dex8-worker/starter.js