Comments (3)
Please note the code below is pseudocode and has not been tested with a compiler.
You can easily wrap this Thunk library with a promise of some kind if you wish to use that method
let promise = Promise()
let thunk = Thunk<MyState> { dispatch, getState in
if getState!.loading {
promise.reject()
return
}
dispatch(RequestStart())
api.getSomething() { something in
if something != nil {
dispatch(RequestSuccess(something))
promise.fulfill()
} else {
dispatch(RequestError())
promise.reject()
}
}
}
store.dispatch(thunk)
promise.then { ... }
You could also write a helper function that creates a thunk with a promise and returns it via a tuple
func foo(...) -> (Thunk<MyState>, promise) { ... }
let (thunk, promise) = foo(...)
store.dispatch(thunk)
promise.then { ... }
If we want to go even further, If you wanted the full behaviour of redux-thunk
with dispatch
able to return promises, you could extend Store
to return promises when a certain type of action is passed in.
struct PromisedThunk<State>: PromisedThunk {
let promise: Promise
let thunk: Thunk<State>
// ...
}
extension Store {
func dispatch(_ thunk: PromisedThunk) -> Promise {
dispatch(thunk.thunk)
return thunk.promise
}
}
from reswift-thunk.
Looks OK, will try something, thanks
from reswift-thunk.
How to chain 2 API calls asynchronous by using thunk ? I tried using Promise but it's not available on Reswift
from reswift-thunk.
Related Issues (17)
- Release HOT 3
- createThunksMiddleware is typo? HOT 5
- Unable to install ExpectThunk using Cocoapods HOT 1
- Export ExpectThunk e.g. to Carthage HOT 2
- Update podspec to support ReSwift 5.0.0 HOT 2
- v.1.2.0 CocoaPods release missing HOT 3
- ExpectThunk fails for watchOS using CocoaPods (pod lib lint) HOT 2
- carthage build fails for non-iOS platforms HOT 4
- ReSwift-Thunk on SPM has different module name HOT 3
- I got conflict between ReSwiftThunk-Core-ExpectThunk and ReSwiftThunk when make archive HOT 2
- Update pod spec for version 2.0.1 HOT 1
- how can i redo or undo a async action
- Can't pass Xcode 12.5.1 build HOT 7
- Can createThunkMiddleware be customized? HOT 4
- Cannot import ReSwiftThunkTesting via SPM HOT 4
- Running thunk with parameters? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reswift-thunk.