# Some common methods

Chaining Promises

```php
function testPromise1() : Promise {
    return new Promise(function ($resolve, $reject) {
        System::setTimeout(function () use ($resolve) {
            $resolve("A");
        }, 1000);
    });
}

function testPromise2() : Promise {
    return new Promise(function ($resolve, $reject) {
        System::setTimeout(function () use ($resolve) {
            $resolve("B");
        }, 1000);
    });
}

function testPromise3() : Promise {
    return new Promise(function ($resolve, $reject) {
        System::setTimeout(function () use ($resolve) {
            $resolve("C");
        }, 1000);
    });
}

function testPromise4() : Promise {
    return new Promise(function ($resolve, $reject) {
        System::setTimeout(function () use ($resolve) {
            $resolve("D");
        }, 1000);
    });
}

testPromise1()->then(function ($value) {
    var_dump($value);
    return testPromise2();
})->then(function ($value) {
    var_dump($value);
    return testPromise3();
})->then(function ($value) {
    var_dump($value);
    return testPromise4();
})->then(function ($value) {
    var_dump($value);
})->catch(function ($value) {
    var_dump($value);
})->finally(function() {
    var_dump("Complete!");
});
```

Promise Arrow

```php
Promise::c(fn($resolve) => $resolve("OK"))->then(fn($result) => var_dump($result));
```

Advanced Mix Async Await + Promise

```php
function request($type) {
    return new Promise(function ($resolve, $reject) use ($type) {
        System::setTimeout(function () use ($resolve, $reject, $type) {
            $type === 'success' ? $resolve('success') : $reject('error');
        }, 1000);
    });
}

function getData() {
    new Async(function () {
        [$data, $error] = Async::await(handleRequest(request('success')));
        if ($error) {
            echo $error;
        } else {
            echo $data;
        }
    });
}

function handleRequest(Promise $promise) {
    return $promise->then(fn($data) => [$data, null])->catch(fn($error) => [null, $error]);
}

getData();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://venndev.gitbook.io/vapm/promise/some-common-methods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
