Class
JSPromise
public final class JSPromise: JSBridgedClass
A wrapper around the JavaScript Promise
class
Relationships
Conforms To
JSBridgedClass
Conform to this protocol when your Swift class wraps a JavaScript class.
Initializers
init(unsafelyWrapping:)
public init(unsafelyWrapping object: JSObject)
This private initializer assumes that the passed object is a JavaScript Promise
init?(_:)
public convenience init?(_ jsObject: JSObject)
Creates a new JSPromise
instance from a given JavaScript Promise
object. If jsObject
is not an instance of JavaScript Promise
, this initializer will return nil
.
init(resolver:)
Creates a new JSPromise
instance from a given resolver
closure.
The closure is passed a completion handler. Passing a successful
Result
to the completion handler will cause the promise to resolve
with the corresponding value; passing a failure Result
will cause the
promise to reject with the corresponding value.
Calling the completion handler more than once will have no effect
(per the JavaScript specification).
Properties
constructor
public static var constructor: JSFunction?
Methods
jsValue()
public func jsValue() -> JSValue
The underlying JavaScript Promise
object wrapped as JSValue
.
construct(from:)
public static func construct(from value: JSValue) -> Self?
Creates a new JSPromise
instance from a given JavaScript Promise
object. If value
is not an object and is not an instance of JavaScript Promise
, this function will
return nil
.
resolve(_:)
public static func resolve(_ value: ConvertibleToJSValue) -> JSPromise
reject(_:)
public static func reject(_ reason: ConvertibleToJSValue) -> JSPromise
then(success:)
@discardableResult
public func then(success: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise
Schedules the success
closure to be invoked on successful completion of self
.
then(success:failure:)
@discardableResult
public func then(
success: @escaping (JSValue) -> ConvertibleToJSValue,
failure: @escaping (JSValue) -> ConvertibleToJSValue
) -> JSPromise
Schedules the success
closure to be invoked on successful completion of self
.
`catch`(failure:)
@discardableResult
public func `catch`(failure: @escaping (JSValue) -> ConvertibleToJSValue) -> JSPromise
Schedules the failure
closure to be invoked on rejected completion of self
.
finally(successOrFailure:)
@discardableResult
public func finally(successOrFailure: @escaping () -> Void) -> JSPromise
Schedules the failure
closure to be invoked on either successful or rejected
completion of self
.