-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatch.go
50 lines (43 loc) · 964 Bytes
/
dispatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
Author: John Connor Sanders
License: Apache Version 2.0
Version: 0.0.3
Released: 06/17/2021
Copyright (c) 2021 John Connor Sanders
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
----------------FETCH--------------------
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
*/
package fetch
import (
"net/http"
)
// Promise struct to store executing thread ...
type Promise struct {
Channel chan *http.Response
Error chan error
httpRequest *http.Request
}
// worker
func (p *Promise) worker(done chan *http.Response, doneErr chan error) {
client := &http.Client{}
resp, err := client.Do(p.httpRequest)
doneErr <- err
done <- resp
<-done
<-doneErr
}
// execute
func (p *Promise) execute() {
done := make(chan *http.Response)
doneErr := make(chan error)
go p.worker(done, doneErr)
p.Channel = done
p.Error = doneErr
}
// dispatch
func dispatch(r *http.Request) *Promise {
promise := Promise{httpRequest: r}
promise.execute()
return &(promise)
}