Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created August 12, 2014 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamingr/98cb672cb289cb53bab6 to your computer and use it in GitHub Desktop.
Save benjamingr/98cb672cb289cb53bab6 to your computer and use it in GitHub Desktop.
func then<NT,NE>(onFulfilled:(T) -> Promise<NT,NE>) -> Promise<NT,NE> {
var p = Promise<NT,NE>()
switch self.state.state {
case .Rejected:
p.reject(self.state.error) // this is a type error
case .Fulfilled:
let result = onFulfilled(self.state.value!)
result.then( { p.fulfill($0) })
result.catch( {p.reject($0) })
case .Pending:
fHandlers.append({ (t:T) in
let res = onFulfilled(t)
res.then({ p.fulfill($0) })
res.catch({ p.reject($0) })
})
}
return p
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment