Skip to content

Commit

Permalink
adding a promise thing
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkmarron committed Nov 29, 2017
1 parent 01a96e2 commit 4f4f654
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions Async-Context-Definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,49 @@ We will see the asynchronous trace:
{event: "executeBegin", ctx: 3, time: 11}
//Prints "Did it"
{event: "executeEnd", ctx: 3, time: 12}
{event: "executeBegin", ctx: 1, time: 13}
{event: "executeBegin", ctx: 2, time: 13}
//Prints "Hello Repeating"
{event: "executeEnd", ctx: 1, time: 14}
{event: "executeEnd", ctx: 2, time: 14}
...
```

### Promise API
Similarly we can provide a basic promise-like API that supports asynchronous context tracking
as follows:
Similarly we can provide a basic promise API that supports asynchronous context tracking
by modifying the real promise implementation as follows: **TODO** this is super rough.
```
function then(onFulfilled, onRejected) {
cfFulfilled = contextify(onFulfilled);
link(cfFulfilled);
cfRejected = contextify(onRejected);
link(cfRejected);
if(this.isResolved) {
cause(cfFulfilled);
cause(cfRejected);
}
...
}
function resolve(value) {
this.handlers.forEach((handler) => cause(cfFulfilled));
...
}
**TODO** promise example here.
function reject(value) {
this.handlers.forEach((handler) => cause(cfFulfilled));
...
}
```
If we invoke this API as follows (when the currentContext is 0):
```
TODO: some promise code goes here.
```
We will see the asynchronous trace:
```
TODO: corresponding trace goes here.
```

These two examples show how the the context relations from
[DLS17](https://www.microsoft.com/en-us/research/wp-content/uploads/2017/08/NodeAsyncContext.pdf)
Expand Down

0 comments on commit 4f4f654

Please sign in to comment.