reactphp / promise

Promises/A implementation for PHP.
https://reactphp.org/promise/
MIT License
2.38k stars 146 forks source link

Added seq method. #159

Closed mmoreram closed 4 years ago

mmoreram commented 4 years ago

Executes each row of an array sequentially. Returns an array of each result.

This PR is not tested yet.

seq([
    $mysql->createTable(),
    $mysql->addElement(),
    $mysql->findElement()
])
->then(array $results) {
    $result[2]->getId();
});
WyriHaximus commented 4 years ago
seq([
    $mysql->createTable(),
    $mysql->addElement(),
    $musql->findElement()
])
->then(array $results) {
    $result[2]->getId();
});

Running that code will execute all 3 operations at the same time and not in sequence. Chaining them this way will execute them in sequence:

$mysql->createTable()->then(function () {
    return $mysql->addElement();
})->then(function () {
    return $musql->findElement();
})->then(array $results) {
    $result[2]->getId();
});
mmoreram commented 4 years ago

Chaining them is what this method does internally. Am I missing something?

WyriHaximus commented 4 years ago

You're chaining the outcome in this method, not the execution. The moment you start an operation it will immediately start doing what you tell it to do. The promise is only a placeholder for the outcome of that operation.

mmoreram commented 4 years ago

Totally right :=(

WyriHaximus commented 4 years ago

The idea itself isn't perse bad FYI ;)

markuszeller commented 4 years ago

Is it correct having $mysql and $musql?

mmoreram commented 4 years ago

@markuszeller fixed. It was a typo

WyriHaximus commented 4 years ago

@markuszeller @mmoreram and I kept it in because it didn't matter for the example you where making :D