Giter Club home page Giter Club logo

Comments (3)

mjarvis avatar mjarvis commented on June 1, 2024

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.

olejnjak avatar olejnjak commented on June 1, 2024

Looks OK, will try something, thanks

from reswift-thunk.

Prateekkotian avatar Prateekkotian commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.