Re: [ServiceWorker] Returning a FetchPromise from fetch() (#625)

@otakustay perhaps this is really out there, but in C#/Unity land when I was using their task/await system (not promise based), I was able to (IMO) elegantly handle situations like this by structuring the code as such (pseudo-code):

```javascript
while (true)
    page = await Q.race(navigate(), page.actions())
```

Apologies since the explanation is a bit cyclic (assume we have "cancelation" to explain how cancelation works). But basically, the idea is given high level primitives that know what to do with cancellation (race which returns the value of the first promise that finishes first and cancels all the other running promises, all which waits for all the promises to finish, etc), then you can describe these relationships in the actual structure of the code vs passing variables around.

So here, navigate would behave as such:

```javascript
function navigate()
{
    return new Promise(function(resolve, reject)
    {
        app.on("navigate", resolve);
    });
}
```

So in other words, navigate never finishes unless the user navigates. When it does navigate, it returns the respective "page" object for said page, which has its own little world of action represented by the actions method. The actions method probably simply never returns on its own (simply waits to get canceled by the navigation). So, no matter what the page is doing, a navigation action necessarily cancels everything, returns a new page, and kicks of the process with the next page's actions. 

With this kind of structure you kind of design your code flow like a flow diagram, with everything thats necessarily coupled at the top. 

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/625#issuecomment-76649998

Received on Monday, 2 March 2015 02:29:39 UTC