Skip to content

Instantly share code, notes, and snippets.

@fatso83
Created August 25, 2015 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fatso83/146ba63ac62f9c7eec01 to your computer and use it in GitHub Desktop.
Save fatso83/146ba63ac62f9c7eec01 to your computer and use it in GitHub Desktop.
Create a faked Promise to use in testing
/**
* How to create a Promise I can control
* when and if it resolves
* @returns {promise, resolve, reject}
*/
function promiseStub(){
var result = {};
result.p = new Promise(function(res,rej){
result.resolve = res;
result.reject = rej;
})
return result;
}
// test
var result = promiseStub();
result.p.then(console.log.bind(console, 'success'),console.error.bind(console, 'failed'))
console.clear();
console.assert(result.p instanceof Promise)
console.assert(result.resolve instanceof Function)
console.assert(result.reject instanceof Function)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment