-
-
Notifications
You must be signed in to change notification settings - Fork 146
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
Comments
No, it is not the same. Chaining $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); |
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 ?
|
This code doesn't chain promises but creates 3 new independent promises. In this case
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);
})
; |
Closing for now, feel free to reopen if you have further questions. |
Are the following codes same?
And
The text was updated successfully, but these errors were encountered: