bluebird promisifyAll() 和 then() 不能与 node require 一起工作
bluebird promisifyAll() and then() not working together with node require
我是第一次使用Promises,还请多多包涵
基本上,我没有看到我的 .then()
语句中的函数被调用。
当我调用 t.t()
时,它是否正常工作。
当我调用t.tAsync()
时,t()
又被调用了。
然而,当我调用 t.tAync().then(console.log);
时,结果并未传递到 then
这是我的节点模块:
'use strict';
var t = function(){
console.log('inside t()');
return 'j';
};
module.exports = {
t: t
};
这是我的演示脚本:
'use strict';
require('should');
var Promise = require('bluebird');
var t = require('../src/t');
Promise.promisifyAll(t);
/*
Call t() once to demonstrate.
Call tAsync() and see t() is called
Call tAsync.then(fn), and then isn't called
*/
// this works as expected, calling t()
console.log('calling t()...' + t.t());
// this also works, calling t()
t.tAsync();
// the then() statement isn't called
t.tAsync().then(function(res){
// I expect this to be called
console.log('HHHUUUZZZAAAHHH' + res);
});
/*
Keep the script running 5 seconds
*/
(function (i) {
setTimeout(function () {
console.log('finished program');
}, i * 1000)
})(5);
这是测试的输出:
inside t()
calling t()...j
inside t()
inside t()
finished program
您的 then
子句将永远不会被调用,因为 tAsync
期望 t
调用回调而不是 return 一个值。
promisifyAll
包装 Node.JS 异步 API 所以它包装的函数需要符合 Node.JS 回调签名:
function t(callback) {
callback(null, 'j');
}
但是,根据您的代码,我怀疑您不想要 promiseifyAll
,而是 try()
或 method()
:
function t() {
return 'j';
}
Promise.try(t).then(function(result) {
console.log(result);
});
或
var t = Promise.method(function() {
return 'j';
});
t().then(function(result) {
console.log(result);
});
为了对比,以上是 Bluebird.js 具体的。要使用通用 Promises 执行此操作,您可以采用以下两种方式之一:
使用 Promise 构造函数:
function t() {
return new Promise(function(resolve, reject) {
resolve('j');
});
}
t().then(function(result) {
console.log(result);
});
或者,使用 then
链接功能:
function t() {
return 'j';
}
Promise.resolve()
.then(t)
.then(function(result) {
console.log(result);
});
我也无法理解 "then" 的工作原理。下面的代码示例(刚刚创建以了解流程)可能对您有所帮助:
var Promise = require('bluebird');
function task1(callback) {
setTimeout(function(){
console.log("taks1");
callback(null, "taks1");
}, 2000);
}
var taks1Async = Promise.promisify(task1);
taks1Async().then(function(result) {
console.log("Current task2, previous: " + result);
return "task2";
})
.then(function(result) {
console.log("Current task3, previous: " + result);
return "task3";
})
.catch(function(e) {
console.log("error: " + e);
});
console.log("Finished");
我是第一次使用Promises,还请多多包涵
基本上,我没有看到我的 .then()
语句中的函数被调用。
当我调用 t.t()
时,它是否正常工作。
当我调用t.tAsync()
时,t()
又被调用了。
然而,当我调用 t.tAync().then(console.log);
这是我的节点模块:
'use strict';
var t = function(){
console.log('inside t()');
return 'j';
};
module.exports = {
t: t
};
这是我的演示脚本:
'use strict';
require('should');
var Promise = require('bluebird');
var t = require('../src/t');
Promise.promisifyAll(t);
/*
Call t() once to demonstrate.
Call tAsync() and see t() is called
Call tAsync.then(fn), and then isn't called
*/
// this works as expected, calling t()
console.log('calling t()...' + t.t());
// this also works, calling t()
t.tAsync();
// the then() statement isn't called
t.tAsync().then(function(res){
// I expect this to be called
console.log('HHHUUUZZZAAAHHH' + res);
});
/*
Keep the script running 5 seconds
*/
(function (i) {
setTimeout(function () {
console.log('finished program');
}, i * 1000)
})(5);
这是测试的输出:
inside t()
calling t()...j
inside t()
inside t()
finished program
您的 then
子句将永远不会被调用,因为 tAsync
期望 t
调用回调而不是 return 一个值。
promisifyAll
包装 Node.JS 异步 API 所以它包装的函数需要符合 Node.JS 回调签名:
function t(callback) {
callback(null, 'j');
}
但是,根据您的代码,我怀疑您不想要 promiseifyAll
,而是 try()
或 method()
:
function t() {
return 'j';
}
Promise.try(t).then(function(result) {
console.log(result);
});
或
var t = Promise.method(function() {
return 'j';
});
t().then(function(result) {
console.log(result);
});
为了对比,以上是 Bluebird.js 具体的。要使用通用 Promises 执行此操作,您可以采用以下两种方式之一:
使用 Promise 构造函数:
function t() {
return new Promise(function(resolve, reject) {
resolve('j');
});
}
t().then(function(result) {
console.log(result);
});
或者,使用 then
链接功能:
function t() {
return 'j';
}
Promise.resolve()
.then(t)
.then(function(result) {
console.log(result);
});
我也无法理解 "then" 的工作原理。下面的代码示例(刚刚创建以了解流程)可能对您有所帮助:
var Promise = require('bluebird');
function task1(callback) {
setTimeout(function(){
console.log("taks1");
callback(null, "taks1");
}, 2000);
}
var taks1Async = Promise.promisify(task1);
taks1Async().then(function(result) {
console.log("Current task2, previous: " + result);
return "task2";
})
.then(function(result) {
console.log("Current task3, previous: " + result);
return "task3";
})
.catch(function(e) {
console.log("error: " + e);
});
console.log("Finished");