0
我在使用browserified bluebird时遇到了问题,问题与promisify函数有关。bluebird promisify issue when browserified
代码:
cat index.js
var Promise = require ('bluebird');
function dosomething (cb) {
cb(null,"I've done something");
}
Promise.promisify(dosomething)()
.then(function(result){
console.log(result);
})
错误:
node bundle.js
/tmp/gulp-test/bundle.js:3880
return makeNodePromisified(callback, receiver, undefined,
^TypeError: undefined is not a function
at promisify (/tmp/gulp-test/bundle.js:3880:12)
at Function.Promise.promisify (/tmp/gulp-test/bundle.js:3894:15)
at Object.bluebird (/tmp/gulp-test/bundle.js:28:9)
at s (/tmp/gulp-test/bundle.js:1:316)
at e (/tmp/gulp-test/bundle.js:1:487)
at Object.<anonymous> (/tmp/gulp-test/bundle.js:1:505)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
gulpfile:
cat gulpfile.js
var browserify = require('browserify');
var gulp = require('gulp');
var uglify = require('gulp-uglify');
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
gulp.task('default', function() {
return browserify('./index.js')
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('./'));
});
个依赖关系:
"dependencies": {
"aws-sdk": "^2.3.4",
"bluebird": "^3.3.5",
"browserify": "^13.0.0",
"gulp": "^3.9.1",
"gulp-uglify": "^1.5.1",
"notp": "^2.0.3",
"thirty-two": "^1.0.1",
"underscore": "^1.8.3",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
}
如果我创建一个承诺手动正常工作:
var Promise = require ('bluebird');
function dosomething (cb) {
cb(null,"I've done something");
}
// manually promisified works fine
function Promisified() {
return new Promise(function(resolve,reject){
dosomething(function(error,result){
if(error){
return reject(error);
}
return resolve(result);
})
})
}
Promisified()
.then(function(result){
console.log(result);
})
我使用的NodeJS 0.12.04而且最新版本(4.4.3)
任何想法?
你已经指出我正确的方向+1,但它并没有解决我的问题。我会接受你的回答,如果你提到''--node'选项browserify哪wroked为我。没有直接工作,因为该选项没有暴露,但我找到了正确的参数:https://github.com/substack/node-browserify/issues/1472#issuecomment-212823393 – Tiago
@Tiago问题是你试图运行一个为服务器上的浏览器优化的脚本。这在服务器上不起作用:当“browserify”运行时,他会使用预编译的“bluebird”浏览器版本 - https://github.com/petkaantonov/bluebird/blob/master/package.json#L70和https:/ /github.com/defunctzombie/node-browser-resolve –
我明白了,但是browserify也有一个选项可以优化节点,那就是解决了我的问题(如果没有你的帮助,我无法做到这一点。 !)。我不打算在浏览器中运行我的代码,但将它与browserify捆绑在一起可以轻松地在AWS lambda上部署。如果你只是改变你的答案,指向'--node'选项browserify我会接受它:) – Tiago