Promisekit: Passing values between then blocks

Created on 12 Jul 2016  路  7Comments  路  Source: mxcl/PromiseKit

Hey Guys,
I am currently working with promiseKit and there is one thing missing.
I also couldn't find a solution inside the documentation.

Is it possible to pass a value from one then block to another.
Let me explain it to you via Code:

somePromise.then { object1 -> Promise<Foo> in
    return FooPromise
}.then { foo -> Promise<Bla> in
    return BlaPromise
}.then { bla -> Void in
    // Here i want to access foo or object1
}.error {

}

I fixed this problem using local variables but I am not sure about doing it this way.
Maybe there is a better approach of solving my issue. Is there an official way of doing this?

My Solution :

var localFoo : Foo?
somePromise.then { object1 -> Promise<Foo> in
    return FooPromise
}.then { foo -> Promise<Bla> in
   localFoo = foo 
    return BlaPromise
}.then { bla -> Void in
   print(localFoo)
}.error {

}

Most helpful comment

somePromise.then { object1 -> Promise<Foo> in
    return FooPromise.then{ ($0, object1) }
}.then { (foo, object1) -> Promise<Bla> in
    return BlaPromise.then{ ($0, foo, object1) }
}.then { (bla, foo, object1) -> Void in
    // TADA!
}.error {

}

Or:

somePromise.then { object1 -> Promise<Foo> in
    return FooPromise.then { foo -> Promise<Bla> in
        return BlaPromise.then { bla -> Void in
            // access foo or object1
        }
    }
}.error {

}

Real world usage may help; generally if you need to access previous values from previous promises鈥搃n my experience at least鈥搕here's a better pattern.

All 7 comments

Only values/objects returned from the immediately preceding then are available in the following then. If you want access to earlier things, you could, for example, construct tuples to pass along in order to avoid leveraging variables at a higher scope. This was just my immediate thought though. May not be the best solution.

somePromise.then { object1 -> Promise<Foo> in
    return FooPromise.then{ ($0, object1) }
}.then { (foo, object1) -> Promise<Bla> in
    return BlaPromise.then{ ($0, foo, object1) }
}.then { (bla, foo, object1) -> Void in
    // TADA!
}.error {

}

Or:

somePromise.then { object1 -> Promise<Foo> in
    return FooPromise.then { foo -> Promise<Bla> in
        return BlaPromise.then { bla -> Void in
            // access foo or object1
        }
    }
}.error {

}

Real world usage may help; generally if you need to access previous values from previous promises鈥搃n my experience at least鈥搕here's a better pattern.

Probably we could expand this section to include this trick: http://promisekit.org/tuples/

Yes, it would be great if you could extend you documentation to include this technique. I will close this issue for now. Thanks for that fast feedback. Have a nice day ..

Could you provide a real-world usage to help us continue to understand the problem?

Thanks for the tip, I was facing this problem when I was trying to:
firstly. Update the user profile
then. Upload the user profile picture
then. Upload user id documents
And in thedone, I wanted to have the response of the user profile update request.

I just tried to apply this pattern in xcode v11.4.1 and got an Illegal Instruction: 4. 馃槵

...
35 swift                    0x000000010a9bbb4e swift::performTypeChecking(swift::SourceFile&, unsigned int) + 1006
36 swift                    0x0000000109ff0ff2 swift::CompilerInstance::parseAndCheckTypesUpTo(swift::CompilerInstance::ImplicitImports const&, swift::SourceFile::ASTStage_t)::$_0::operator()(swift::SourceFile&) const + 66
37 swift                    0x0000000109fefecf swift::CompilerInstance::performSemaUpTo(swift::SourceFile::ASTStage_t) + 6687
38 swift                    0x0000000109cdbd99 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 10281
39 swift                    0x0000000109c5cf53 main + 1283
40 libdyld.dylib            0x00007fff6b6ffcc9 start + 1
41 libdyld.dylib            0x0000000000000050 start + 2492466056
error: Illegal instruction: 4 (in target 'MyApp' from project 'MyApp')
Was this page helpful?
0 / 5 - 0 ratings

Related issues

adrianbg picture adrianbg  路  6Comments

Banck picture Banck  路  6Comments

dboydor picture dboydor  路  4Comments

mxcl picture mxcl  路  4Comments

ghost picture ghost  路  3Comments