我寫測試代碼爲Bluebird在Node.js的是這樣的:爲什麼結果不是我期望的Nodejs承諾?
var Promise = require(‘bluebird’)
var obj = {
func1: function() {
return ‘foo’
},
func2: function() {
return ‘bar’
}
}
console.log("==================================")
Promise.promisifyAll(obj)
obj.func1Async().then(function (result) {
console.log(result)
})
我所期待的是打印的「富」,但實際上沒有輸出。 有什麼問題嗎?
Ovidiu的答案將解決您的問題:-)另外,您可以:'obj.func1Async()。then(console.log)'並避免一些樣板代碼。 – Sergeon
我不明白,我認爲是 'obj.func1Async()'返回一個承諾,並可以調用then方法,然後傳遞返回值「foo」。像fs:'fs.readFileAsync('filename')。then(function(content){console.log(content)})' 有什麼區別? – TreeCatCat