Skip to content

Instantly share code, notes, and snippets.

@rainabba
Last active September 23, 2015 19:18
Show Gist options
  • Save rainabba/b83eed7f6317e5e7d945 to your computer and use it in GitHub Desktop.
Save rainabba/b83eed7f6317e5e7d945 to your computer and use it in GitHub Desktop.
Example and differences for A+ Promise using Resolve/Reject and a Bluebird promisify wrapper
<html>
<head>
<script src="https://www.promisejs.org/polyfills/promise-7.0.1.min.js"></script>
<script src="http://requirejs.org/docs/release/2.1.2/minified/require.js"></script>
</head>
<body>
<h1>Open the console</h1>
<script>
//// Here is a deferred approach using the Bluebird Promisify wrapper
// callback is provided by the promisfy wrapper and data is provided by the call to the promisified function.
function funcToPromisify( data, callback ) {
//do something with data
console.log('running:', data);
callback( null , data ); // first parm is used to throw errors to be handled by catch block below
}
require(['https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.10.1/bluebird.js'], function( Promise ) {
var testPromisify = Promise.promisify( funcToPromisify );
testPromisify(( { foobar: 1 } ))
.then( function( data ) {
console.log( 'then', data );
}).catch(function(e) {
console.log("Error:", e);
});
});
</script>
</body>
</html>
<html>
<head>
<script src="https://www.promisejs.org/polyfills/promise-7.0.1.min.js"></script>
<script src="http://requirejs.org/docs/release/2.1.2/minified/require.js"></script>
</head>
<body>
<h1>Open the console</h1>
<script>
//// Here is a deferred approach using the Bluebird Promisify wrapper
// callback is provided by the promisfy wrapper and data is provided by the call to the promisified function.
function funcToPromisify( data, callback ) {
//do something with data
console.log('running:', data);
callback( { foo: 1 } , data );
}
require(['https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.10.1/bluebird.js'], function( Promise ) {
var testPromisify = Promise.promisify( funcToPromisify );
testPromisify(( { foobar: 1 } ))
.then( function( data ) {
console.log( 'then', data );
}).catch(function(e) {
console.log("Error:", e);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment