Skip to content

Instantly share code, notes, and snippets.

@kflu
Created May 12, 2016 01:26
Show Gist options
  • Save kflu/d571d792ad579922800f17f48b78e425 to your computer and use it in GitHub Desktop.
Save kflu/d571d792ad579922800f17f48b78e425 to your computer and use it in GitHub Desktop.
Test various error situation with promises and async/await
#!/usr/bin/env node
require('babel-polyfill');
var PromiseBB = require('bluebird');
// useless if the lost rejection Promise is not coming from BB
PromiseBB.onPossiblyUnhandledRejection(function(error) {
throw error;
});
function f() {
console.log("Rejecting!!");
//throw "throw!";
return Promise.reject("oops!");
}
async function g() {
await f();
}
async function h() {
f(); // oops, I forgot to type "await"
}
function writeValue(val) {
console.log("Success! val: %s", val);
}
function writeError(err) {
console.log("Error! err: %s", err);
}
//g().then(writeValue, writeError);
//h().then(writeValue, writeError);
//Promise.resolve(g()).then(writeValue, writeError);
//Promise.resolve(h()).then(writeValue, writeError);
//PromiseBB.resolve(g()).then(writeValue, writeError);
PromiseBB.resolve(h()).then(writeValue, writeError);
console.log("Some logic to continue...");
/*
* 1. Always use bb to reolsve at the end. Don't trust the promise is a bb promise or other promise
* 2. AND... always check if you forgot "await"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment