I'm really hoping that I've just continually missed something obvious here, but after trying for hours to use promisify, I've distilled things down to a VERY simple example that only logs "running" and never "then". What have I missed or is this a bug?
<html>
<head>
<script src="http://requirejs.org/docs/release/2.1.2/minified/require.js"></script>
</head>
<body>
<script>
require(['https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.10.1/bluebird.js'], function( Promise ) {
var initServer = Promise.promisify( function( foobar ) {
console.log( 'running' );
return true;
} )( { foobar: 1 } ).then( function( ) {
console.log( 'then' );
});
});
</script>
</body>
</html>
@rainabba You're calling promisify on a normal function, but promisify is designed to work with node style callbacks.
You almost certainly want:
var initServer = new Promise(function (resolve, reject) {
console.log('running');
resolve(true);
})
.then(function () {
console.log("then");
});
"promisify is designed to work with node style callbacks" is the critical point I was missing. I was given an example (here)https://github.com/hapijs/hapi/issues/2516 that made me think the approach I was trying was possible.
Thanks for clarifying.
For anyone else that might run across this and find the example useful, what I really needed was:
function initServer( foobar ) {
return new Promise( function (resolve, reject) {
console.log('running');
resolve( foobar );
});
}
initServer( { foobar: 1 } ).then( function ( foobar ) {
console.log( 'then:', foobar );
return foobar;
});
@phpnode Any chance you could adapt my original example to work with a "node style callback" so I can be sure I understand what that means. I feel like I've got an idea until I try to setup that example myself :) Would be very appreciated.
_for illustration purposes, don't actually do this, use .promisifyAll() instead_
const fs = require("fs");
const stat = Bluebird.promisify(fs.stat);
stat(__filename).then(console.log);
haha. That fs example is why I've not understood sooner. Not ONE example out there is standalone (and thus clear). Thank you though, I did figure it out and am leaving it here for anyone else that might find it useful:
Here is a Gist that covers both approaches with errors: https://gist.github.com/rainabba/b83eed7f6317e5e7d945
Most helpful comment
haha. That fs example is why I've not understood sooner. Not ONE example out there is standalone (and thus clear). Thank you though, I did figure it out and am leaving it here for anyone else that might find it useful:
Here is a Gist that covers both approaches with errors: https://gist.github.com/rainabba/b83eed7f6317e5e7d945