# ASAPs

## Introduction

Asaps are very similar to promises, with two major differences:

* They can be resolved in the same thread.
* They can be cancelled

But other than that are fully compatible with promises

{% embed url="<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise>" %}

## Synchronous Execution

One of the main drives to create the ASAPs was to have an abstraction that will encapsulate promises, but that would also run them synchronously when possible.

Let's illustrate this with an example:

```javascript
var thePro = new Promise ((resolve) => resolve(5));
thePro.then ((value) => console.log(value))
console.log('hi!')

//This will print
//hi!
//5
```

As you can see, the promise resolves immediately, but the order of the execution is not what you might expect.

**Every time you use a promise, the code to resolve the promise will be run as if it was in a different not concurrent thread**

Let's compare it with an Asap.

```javascript
var thePro = Asaps.now(5);
thePro.then ((value)=>console.log(value))
console.log('hi!')

//This will print
//5
//hi!
```

In here, the Asap resolves, well.. asap&#x20;

### Why?

The goal is to simplify writing code that sometimes should resolve now, sometimes should resolve later...

If you are familiar with other state management frameworks like Redux, you might be a know a side effect of this, when you perform a synchronous operation that changes the state, but you can't query the state immediately after.

This small inconvenience then starts leaking all over the framework, all of the sudden you have to write callbacks for everything to guarantee that the state you want to read is up to date.

With ConanJs you will be able to query directly the state after performing an action that updates it synchronously. This is because ASAPs are used behind the scenes.

## Creating ASAPS

You can create an Asap through the Asaps class. In here you can find its main 3 methods.

{% hint style="success" %}
To see all the available methods, like resolving an Asap on the back of a fetch check the [Asaps API docs](/api/asaps/asaps.md).&#x20;
{% endhint %}

### fromPromise

If you have a promise, all you have to do is use this method to create an Asap from it.

```javascript
let myAsap = Asasp.fromPromise(myPromise) 
```

### now

Lets you create a promise that resolves now to a value

```javascript
let myAsap = Asasp.now(5) 
```

### next

Lets you create a promise that will be resolved synchronously, but it also lets you defer the value to resolve later on the same thread.

This method is the cornerstone of the reason of Asaps to exist, let's illustrate this with an example.

```javascript
// returns a tuple, the first value is the atual asap
// the second is a function to resolve the value
let [asap, next] = Asasp.next() 

console.log(1)
asap.then((value)=>console.log(value))
next(2)
console.log(3)

// This will print
// 1
// 2
// 3
```

## Life Cycle Interface

Asaps have a life cycle that matches the one for promises, with the added state of the Asap being cancelled.

Note that it is out of the scope of this documentation to explain how a promise works, so we are going to assume that you are familiar with them.

The two main life cycle methods for a promise are then and catch, in ConanJs we also have onCancel.

### then

Similar to a normal promise, you can append listeners to execute once that the promise is resolved successfully.

### catch

These listeners will be executed in case of an error triggered while resolving the underlying Asap.

### onCancel

Lastly, you can also add listeners to be executed when the promise has been cancelled.

{% hint style="success" %}
As of v1.0 we don't have a finally callback, but this would be added soon.
{% endhint %}

## Cancelling Asaps

Whether a promise should be cancellable or not will likely spark flame wars, and we do understand why:

* If you cancel an async operation, but you don't cancel everything that operation has started, can it really be cancelled?
* How do you cancel a network call already started?

These are very difficult questions to answer and they have stalled any potential progress into providing this mechanism built in for Promises.

But we think that this is not fair for the developer, that an operation is dangerous and delicate, should not mean that is also very complicated to trigger.

That is why we provide with [**asap.cancel ()**.](/api/asaps/asap.md) if you call it, it will not resolve (then and catch will not be invoked), and instead all onCancel listeners will be invoked.

Would this mean that is going to cancel everything that you started? **NO**

You will need to make sure that you react to the cancellation according to your business use case, and do as much housekeeping as needed, but at least you get to choose if you want to go down this route.


---

# 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://docs.conanjs.io/asaps.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.
