Skip to content

Instantly share code, notes, and snippets.

@pmarrapese
Created May 15, 2016 00:32
Show Gist options
  • Save pmarrapese/71c2560a1d0dad2d211283f03125ccc3 to your computer and use it in GitHub Desktop.
Save pmarrapese/71c2560a1d0dad2d211283f03125ccc3 to your computer and use it in GitHub Desktop.
Bluebird vs. Native Promises

Bluebird vs. Native Promises

Stack traces

Native

"use strict";

function doAsyncThing() {
  let p = new Promise((resolve, reject) => process.nextTick(resolve)).then(() => {
    console.log('after resolve', new Error().stack);
  });
}

console.log('starting');
doAsyncThing();
console.log('out of main');

Stack trace is lost in the callback.

starting
out of main
after resolve Error
    at Promise.then (H:\src\node-test\src\app.js:5:38)
    at process._tickCallback (internal/process/next_tick.js:103:7)
    at Function.Module.runMain (module.js:577:11)
    at startup (node.js:160:18)
    at node.js:445:3

Bluebird

"use strict";

global.Promise = require('bluebird');
Promise.config({ longStackTraces: true });

function doAsyncThing() {
  let p = new Promise((resolve, reject) => process.nextTick(resolve)).then(() => {
    console.log('after resolve', p._trace);
  });
}

console.log('starting');
doAsyncThing();
console.log('out of main');

Bluebird library preserves the stack trace internally.

starting
out of main
after resolve { Error
    at Promise.longStackTracesCaptureStackTrace [as _captureStackTrace] (H:\src\node-test\node_modules\bluebird\js\release\debuggability.js:369
:19)
    at Promise._then (H:\src\node-test\node_modules\bluebird\js\release\promise.js:228:17)
    at Promise.then (H:\src\node-test\node_modules\bluebird\js\release\promise.js:123:17)
    at doAsyncThing (H:\src\node-test\src\app.js:7:71)
    at Object.<anonymous> (H:\src\node-test\src\app.js:13:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3
  _parent: undefined,
  _promisesCreated: 0,
  _length: 1,
  _promiseCreated: null }

Unhandled error behavior

"use strict";

function runBadPromise() {
  let p = new Promise((resolve, reject) => {
     throw new Error('exception inside promise');
  });
}

function runBadResolve() {
  let p = new Promise((resolve, reject) => process.nextTick(resolve)).then(() => {
    throw new Error('exception inside resolve');
  });
}

console.log('starting');
runBadPromise();
runBadResolve();
console.log('out of main');

Native

Exceptions are not handled.

starting
out of main

Bluebird

starting
out of main
Unhandled rejection Error: exception inside promise
    at Promise (H:\src\node-test\src\app.js:8:12)
    at runBadPromise (H:\src\node-test\src\app.js:7:11)
    at Object.<anonymous> (H:\src\node-test\src\app.js:19:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3

Unhandled rejection Error: exception inside resolve
    at Promise.then (H:\src\node-test\src\app.js:14:11)
    at tryOnImmediate (timers.js:543:15)
    at processImmediate [as _immediateCallback] (timers.js:523:5)
From previous event:
    at runBadResolve (H:\src\node-test\src\app.js:13:71)
    at Object.<anonymous> (H:\src\node-test\src\app.js:20:1)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:445:3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment