使用 promisifyAll 承诺一个已经承诺的函数
promisifying an already promisified function with promisifyAll
根据关于 promisification 的 Bluebird 文档:
If the object already has a promisified version of the method, it will be skipped.
事实上,promisifyAll()
还创建了一个已 promisified 函数的 Async
版本,预计为:
idempotent in the sense that promisified functions are instantly returned back.
正如问题 Idempotent promisify #159
中所指出的
因此在下面的示例中,我希望 obj.foo()
和 obj.fooAsync()
具有相同的结果。
let obj = {
foo: function(){
return Promise.resolve('I am foo!!!')
}
}
obj = Promise.promisifyAll(obj)
obj.foo().then(msg => console.log(msg)) // > I am foo
obj.fooAsync().then(msg => console.log(msg)) // prints nothing
我误解了哪一部分?
运行 示例 here
不是真正的答案,但我确实发现了一些东西......不确定它是什么意思,但我想我会分享:
const Promise = require('bluebird');
const obj = {
foo: () => {
return new Promise((resolve)=> {
resolve('Hello!!!');
});
}
};
const new_obj = Promise.promisifyAll(obj);
console.log(new_obj.foo());
console.log(new_obj.fooAsync());
console.log(new_obj);
Returns:
Promise {
_bitField: 33554432,
_fulfillmentHandler0: undefined,
_rejectionHandler0: 'Hello!!!',
_promise0: undefined,
_receiver0: undefined }
Promise {
_bitField: 134217728,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
{ foo: [Function: foo], fooAsync: [Function: ret] }
问题:
- 为什么都是
_fulfillmentHandler0: undefined
?
- 为什么
foo
的 _rejectionHandler0
是我们期望 _fulfillmentHandler0
的 'Hello!!!'
的 return?
- 为什么是
foo: [Function: foo]
和fooAsync: [Function: ret]
?
Which part am I misunderstanding?
你的 foo
功能已经被承诺了。这不是 - 它只是 returns 一个承诺。正如 Petka points out,“不可能在代码中判断某个函数是否是承诺返回函数。”(至少在一般意义上,有 一些我们可以告诉的功能)。
成为 "promisified",尤其是在 idempotent Promise.promisify
function, refers to a function that is the result of a Promise.promisify
call (or was created through Promise.promisifyAll
). It's about this part of the code that checks whether the function is tagged as being bluebird-created.
的背景下
更好的演示:
const obj = {
foo(callback) {
callback(null, 'I am foo!!!')
}
};
Promise.promisifyAll(obj);
obj.fooAsync().then(console.log) // > I am foo
// so far so good
Promise.promisifyAll(obj);
console.assert(typeof obj.fooAsyncAsync == "undefined"); // it didn't promisify again
console.assert(obj.fooAsync === Promise.promisify(obj.fooAsync));
根据关于 promisification 的 Bluebird 文档:
If the object already has a promisified version of the method, it will be skipped.
事实上,promisifyAll()
还创建了一个已 promisified 函数的 Async
版本,预计为:
idempotent in the sense that promisified functions are instantly returned back.
正如问题 Idempotent promisify #159
中所指出的因此在下面的示例中,我希望 obj.foo()
和 obj.fooAsync()
具有相同的结果。
let obj = {
foo: function(){
return Promise.resolve('I am foo!!!')
}
}
obj = Promise.promisifyAll(obj)
obj.foo().then(msg => console.log(msg)) // > I am foo
obj.fooAsync().then(msg => console.log(msg)) // prints nothing
我误解了哪一部分?
运行 示例 here
不是真正的答案,但我确实发现了一些东西......不确定它是什么意思,但我想我会分享:
const Promise = require('bluebird');
const obj = {
foo: () => {
return new Promise((resolve)=> {
resolve('Hello!!!');
});
}
};
const new_obj = Promise.promisifyAll(obj);
console.log(new_obj.foo());
console.log(new_obj.fooAsync());
console.log(new_obj);
Returns:
Promise {
_bitField: 33554432,
_fulfillmentHandler0: undefined,
_rejectionHandler0: 'Hello!!!',
_promise0: undefined,
_receiver0: undefined }
Promise {
_bitField: 134217728,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
{ foo: [Function: foo], fooAsync: [Function: ret] }
问题:
- 为什么都是
_fulfillmentHandler0: undefined
? - 为什么
foo
的_rejectionHandler0
是我们期望_fulfillmentHandler0
的'Hello!!!'
的 return? - 为什么是
foo: [Function: foo]
和fooAsync: [Function: ret]
?
Which part am I misunderstanding?
你的 foo
功能已经被承诺了。这不是 - 它只是 returns 一个承诺。正如 Petka points out,“不可能在代码中判断某个函数是否是承诺返回函数。”(至少在一般意义上,有 一些我们可以告诉的功能)。
成为 "promisified",尤其是在 idempotent Promise.promisify
function, refers to a function that is the result of a Promise.promisify
call (or was created through Promise.promisifyAll
). It's about this part of the code that checks whether the function is tagged as being bluebird-created.
更好的演示:
const obj = {
foo(callback) {
callback(null, 'I am foo!!!')
}
};
Promise.promisifyAll(obj);
obj.fooAsync().then(console.log) // > I am foo
// so far so good
Promise.promisifyAll(obj);
console.assert(typeof obj.fooAsyncAsync == "undefined"); // it didn't promisify again
console.assert(obj.fooAsync === Promise.promisify(obj.fooAsync));