So I import import PromiseKit and then try
FIRDatabase.database().reference().child("somechild").removeValue().then {
//…
}
obviously this doesnt work, and i was wondering what I'am missing to make promises work with firebase
What I'am trying to accomplish is to remove four firebase references all at once with a single catch method. With nodeJs I would easily use:
Promise.all ([
someRef.remove(),
someRef.remove(),
someRef.remove(),
someRef.remove()
]).then (function({
//…
}).catch({
//handle error
})
what is the equivalent for that in swift
Check out when(fulfilled:).
So I import import PromiseKit and then try
FIRDatabase.database().reference().child("somechild").removeValue().then {…}
obviously this doesnt work...
We don't currently have an extension for Firebase the way we do for e.g. Alamofire, so we have no out-of-the-box functionality like this unfortunately. We probably should, as we used to for Parse and Firebase is the common replacement. We make this kind of stuff easy though. For example, to create an overload of Firebase's removeValue() that returns a Promise:
extension FIRDatabaseReference {
func removeValue() -> Promise<FIRDatabaseReference> {
return PromiseKit.wrap { resolve in
removeValue(completionBlock: resolve)
}
}
}
Then you can use removeValue() as in your example above. This will also, in turn, let you utilize our when, as @zlangley mentioned, which acts as Promise.all from your example:
```swift
PromiseKit.when(fulfilled: [someRef1.removeValue(), someRef2.removeValue(), someRef3.removeValue(), someRef4.removeValue()]).then { values in
//…
}
````
Take note of the two variants of when though (when(resolved:) and when(fulfilled:)) as they behave differently.
Most helpful comment
We don't currently have an extension for Firebase the way we do for e.g. Alamofire, so we have no out-of-the-box functionality like this unfortunately. We probably should, as we used to for Parse and Firebase is the common replacement. We make this kind of stuff easy though. For example, to create an overload of Firebase's
removeValue()that returns a Promise:Then you can use
removeValue()as in your example above. This will also, in turn, let you utilize ourwhen, as @zlangley mentioned, which acts asPromise.allfrom your example:```swift
PromiseKit.when(fulfilled: [someRef1.removeValue(), someRef2.removeValue(), someRef3.removeValue(), someRef4.removeValue()]).then { values in
//…
}
````
Take note of the two variants of
whenthough (when(resolved:)andwhen(fulfilled:)) as they behave differently.