簡體   English   中英

bluebird是否具有用於在promises中包裝函數的說服功能?

[英]Does bluebird have a convince function for wrapping functions in promises?

我有一個功能對象。 這些功能並不總是完全承諾。

例如:

function helloWorld(string){
  if(string == "foo") throw new Error("string is foo")
  return aPromise(string)
}

如果string是“foo”,那么這將拋出一個不會被catch調用捕獲的錯誤。

我寧願寫上面更清晰的代碼,而不是這樣:

function helloWorld(string){
  return Promise.resolve(){
    if(string == "foo") throw new Error("string is foo")
    return aPromise(string)
  }
}

所以我創建了這些映射在對象上的函數,並添加了嵌套的Promise.resolve

makePromises: function(obj){
  return _.mapObject(obj, function(fn){
    return function(){
      var args = _.values(arguments)
      return Promise.resolve().then(function(){
        return fn.apply(null, args)
      })
    }
  })
},
makeNestedPromises:function(obj){
  return _.mapObject(obj, function(fn){
    return function(){
      var args = _.values(arguments)
      var value = fn.apply(null, args)
      return function(){
        var args = _.values(arguments)
        return Promise.resolve().then(function(){
          return value.apply(null, args)
        })
      }
    }
  })
}

我想知道這是否已經存在於像promisifyAll這樣的promisifyAll但是對於看似同步的函數(沒有回調),或者其他人認為這有用。

所以似乎promisify這樣做,只是沒有我喜歡的嵌套函數。

var Promise = require("bluebird")

var concat = function(one, two){
  return Promise.resolve(one + " " + two)
}

var fns = {}

fns.hello = function(name, rank){
  if(name == "tom") throw new Error("tom unauthorized")
  return concat(name, rank)
}

Promise.promisifyAll(fns)

fns.helloAsync("tom", "developer")
  .catch(function(e){
    console.log(e)
  })

這是破碎的:

var Promise = require("bluebird")

var concat = function(one, two){
  return Promise.resolve(one + " " + two)
}

var fns = {}

fns.hello = function(unauthorized){
  return function(name, rank){
    if(name == unauthorized) throw new Error("unauthorized")
    return concat(name, rank)
  }
}

Promise.promisifyAll(fns)

// here promisify thinks that the top level function should be a promise
fns.helloAsync("tom")("tom", "developer")
  .catch(function(e){
    console.log(e)
  })

是的,您可以使用Promise.method

var helloWorld = Promise.method(function helloWorld(string) {
  if(string == "foo") throw new Error("string is foo")
  return aPromise(string)
});

您應該僅對具有異步回調的函數使用promisification,它不適用於同步或承諾返回的函數(即使它確實捕獲了同步異常)。

我創建了一個帶有deep可選參數的Promise.methodAll 使用Promise.method從@ BERGI的答案。

var Promise = require("bluebird")
var _ = require("underscore")

Promise.methodAll = function(obj, deep){
  return _.mapObject(obj, function(fn){
    if(!deep) return Promise.method(fn)
    return function(){
      var nestedFn = fn.apply(null, _.values(arguments))
      return Promise.method(nestedFn)
    }
  })
}

var concat = function(one, two){
  return Promise.resolve(one + " " + two)
}

var deep = {}

deep.authorize = function(unauthorized){
  return function(name, rank){
    if(name == unauthorized) throw new Error("unauthorized")
    return concat(name, rank)
  }
}

deep = Promise.methodAll(deep, true)

var normal = {}

normal.authorize = function(name, rank){
  if(name == "tom") throw new Error("tom unauthorized")
  return concat(name, rank)
}

normal = Promise.methodAll(normal)

normal.authorize("tom", "developer")
  .catch(function(e){
    console.log(e) //[Error: tom unauthorized]
  })

deep.authorize("tom")("tom", "developer")
  .catch(function(e){
    console.log(e) //[Error: unauthorized]
  })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM