Robert Bluschke
Robert Bluschke

Reputation: 11

Own Promisekit promises do not response properly

I wrote my own promise and want to chain it, but the result is always only the first in the chain. I think there is an understanding issue on my side how to use them, but I don't able to find it.

My Code:

-(PMKPromise*)tryTheFirstPromiseWorkflow:(TestCycleObject *)testCycleObject{

PMKPromise *promise = [PMKPromise new:^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject) {
    fulfill(PMKManifold([_addModul addSomething:testCycleObject]));
}];

promise.then(^(TestCycleObject *testCycleObject){
    testCycleObject = [_addModul addSomething:testCycleObject];
    NSLog(@"Result: %i, Fulfilled: %d", testCycleObject.result, promise.fulfilled);
    return testCycleObject;

}).then(^(TestCycleObject * testCycleObject){
    testCycleObject = [_multiModul multiSomething:testCycleObject];
    NSLog(@"Result: %i, Fulfilled: %d", testCycleObject.result, promise.fulfilled);
    return testCycleObject;
}).then(^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject){
    fulfill(testCycleObject);
});
return promise;}

At first I create the promise, then the chain starts, but after creating the promise it will deliver only the action passed in the promise initialization and not the chain as result.

I tried your purpose, but still run into an issue:

-(void)test{
  PMKPromise *test = (PMKPromise*)[self tryTheFirstPromiseWorkflow:  [[TestCycleObject alloc] initWithDefaultValues]];
  TestCycleObject *temp = (TestCycleObject*) test.value;
  NSLog(@"Test: %i", temp.result);
}

This is how I call the promise, but the result of it is always empty. The promise is executed after my log, but then I can't fetch the result. So how can I get the result in my method. I thought the method have to stop until the promise is running, am I wrong?

Upvotes: 1

Views: 851

Answers (2)

mxcl
mxcl

Reputation: 26893

The other answer was correct:

- (PMKPromise*)tryTheFirstPromiseWorkflow:(TestCycleObject *)testCycleObject{
    return [PMKPromise new:^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject) {
        fulfill(PMKManifold([_addModul addSomething:testCycleObject]));
    }].then(^(TestCycleObject *testCycleObject){
        testCycleObject = [_addModul addSomething:testCycleObject];
        NSLog(@"Result: %i, Fulfilled: %d", testCycleObject.result, promise.fulfilled);
        return testCycleObject;
    }).then(^(TestCycleObject * testCycleObject){
        testCycleObject = [_multiModul multiSomething:testCycleObject];
        NSLog(@"Result: %i, Fulfilled: %d", testCycleObject.result, promise.fulfilled);
        return testCycleObject;
    }).then(^(PMKPromiseFulfiller fulfill, PMKPromiseRejecter reject){
        fulfill(testCycleObject);
    });
}

You were returning the FIRST promise. So you would only get its result. You have to return the promise from the then to get the last result.

Each then returns a new promise, this is a key understanding with promises, but it's very common that people don't realize this. Each new promise represents the asynchronous task for that then block in the chain.

Being able to then of a promise earlier in the chain is sometimes very useful.

Upvotes: 1

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276446

Promises are immutable, when you add then to a promise it will not modify the original promise but create a new one.

Instead, you need to return the promise.then of the blocks:

// note the `return here`
return promise.then(^(TestCycleObject *testCycleObject){
    testCycleObject = [_addModul addSomething:testCycleObject];
    NSLog(@"Result: %i, Fulfilled: %d", testCycleObject.result, promise.fulfilled);
    return testCycleObject;

}).

Upvotes: 1

Related Questions