Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question #89

Closed
sm2017 opened this issue Feb 22, 2017 · 4 comments
Closed

Question #89

sm2017 opened this issue Feb 22, 2017 · 4 comments
Labels

Comments

@sm2017
Copy link

sm2017 commented Feb 22, 2017

Are the following codes same?

$promise= $deferred->promise()
    ->then(function () {
          // Logic codes
    })
    ->then(function () {
          // Logic codes
    })
    ->then(function () {
          // Logic codes
    });

And

$promise= $deferred->promise()
    ->then(function () {
          // Logic codes
    });
$promise->then(function () {
          // Logic codes
    });
$promise->then(function () {
          // Logic codes
    });
@jsor
Copy link
Member

jsor commented Feb 22, 2017

No, it is not the same. Chaining then() calls allows for transformation of the resolution value. See https://github.com/reactphp/promise/blob/master/README.md#resolution-forwarding.

$promise = $deferred->promise()
    ->then(function ($x) {
          // Here we get the initial resolution value: $x === 1;
          // Now add 1
          return $x + 1;
    })
    ->then(function ($x) {
          // Here we get the transformed value from the previous then() callback: $x === 2;
          // Now add 1 again
          return $x + 1;
    })
    ->then(function ($x) {
          // Here we get the transformed value from the previous then() callback: $x === 3;
          echo $x;
    });

$deferred->resolve(1);

@sm2017
Copy link
Author

sm2017 commented Feb 23, 2017

How exactly this code runs?

$promise = $deferred->promise();
$promise->then(function ($x) {
          // Here we get the initial resolution value: $x === 1;
          // Now add 1
          return $x + 1;
    });
$promise->then(function ($x) {
          // Here we get the transformed value from the previous then() callback: $x === 2;
          // Now add 1 again
          return $x + 1;
    });
$promise->then(function ($x) {
          // Here we get the transformed value from the previous then() callback: $x === 3;
          echo $x;
    });

$deferred->resolve(1);

How can I have a conditional then ?

Get a promise
Check redis , if found cached data , use it 
Else Fetch a row from database and then If(0<expireTime) then cache it to redis
then ...

@jsor
Copy link
Member

jsor commented Feb 23, 2017

How exactly this code runs?

This code doesn't chain promises but creates 3 new independent promises. In this case $x === 1 in all 3 callbacks.

How can I have a conditional then ?

Get a promise
Check redis , if found cached data , use it 
Else Fetch a row from database and then If(0<expireTime) then cache it to redis
then ...

Something like this maybe:

function fetchFromDbAndStoreToRedis($key)
{
    return queryDb($key)
        ->then(function($row) use ($key) {
            $value = $row['value'];

            if ($row['expireTime'] <= 0) {
                return $value;
            }

            return storeToRedis($key, $value, $row['expireTime'])
                ->then(function() use ($value) {
                    return $value;
                });
        })
    ;
}

fetchFromRedis($key)
    ->then(function($value) use ($key) {
        if (null !== $value) {
            return $value;
        }

        return fetchFromDbAndStoreToRedis($key);
    })
;

@jsor
Copy link
Member

jsor commented Mar 10, 2017

Closing for now, feel free to reopen if you have further questions.

@jsor jsor closed this as completed Mar 10, 2017
@clue clue added the question label Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants