You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Current (2015-01-08) behaviour is that every callback (from map, then and the like) happen synchronously on the same thread as where PromiseSource.resolve is called. Lets call this "synchronous dispatch".
Sometimes synchronous dispatch is the correct behaviour, but most of the time it leads to unexpected results.
Proposal: add dispatchOn
Add support to "configure" a promise to run on a provided dispatch queue. For example:
let myQueue = dispatch_queue_create("com.example.MyQueue", nil);
getSomePromise()
.dispatchOn(myQueue)
.map { value in
// This is called on my queue
return value
}
.then { value in
// This is also called on my queue
// That means we can't do UI operations! (not UI thread)
}
The call to dispatchOn(queue: dispatch_queue_t) returns a new Promise that is correctly configured (i.e. it doesn't mutate the existing promise). This way we can also "switch" queues:
getSomePromise()
.dispatchOn(myQueue)
.map { value in
// This is called on my queue
return value
}
.dispatchOn(dispatch_get_main_queue())
.then { value in
// This runs on main thread, so we can do UI operations
}
When nothing is configured, the main queue is chosen as a default (so users can do UI operations):
getSomePromise()
.then { value in
// Default: run on main thread
}
However, if for some reason, you want to dispatch synchronously, this can be done using dispatchSync():
getSomePromise()
.dispatchSync()
.then { value in
// This now runs on whatever thread `PromiseSource.resolve` was called on.
// This can be used if the caller of `resolve` expects the side effects from `then` to have happend.
}
Running a "sub computation" on a different queue is also possible, using the existing flatMap:
getSomePromise()
.dispatchOn(myQueue)
.flatMap { value in
return Promise(value: value)
.dispatchOn(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))
.map { val in
// This runs on a high priority queue
return val
}
}
.then { value in
// This is also called on my queue
// That means we can't do UI operations! (not UI thread)
}
The text was updated successfully, but these errors were encountered:
Current (2015-01-08) behaviour is that every callback (from
map
,then
and the like) happen synchronously on the same thread as wherePromiseSource.resolve
is called. Lets call this "synchronous dispatch".Sometimes synchronous dispatch is the correct behaviour, but most of the time it leads to unexpected results.
Proposal: add dispatchOn
Add support to "configure" a promise to run on a provided dispatch queue. For example:
The call to
dispatchOn(queue: dispatch_queue_t)
returns a new Promise that is correctly configured (i.e. it doesn't mutate the existing promise). This way we can also "switch" queues:When nothing is configured, the main queue is chosen as a default (so users can do UI operations):
However, if for some reason, you want to dispatch synchronously, this can be done using
dispatchSync()
:Running a "sub computation" on a different queue is also possible, using the existing
flatMap
:The text was updated successfully, but these errors were encountered: