Skip to content

Commit

Permalink
fix deferred antipattern (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamingr authored and mikeal committed Nov 20, 2017
1 parent 642b154 commit 012ce00
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 18 deletions.
Binary file added .DS_Store
Binary file not shown.
20 changes: 2 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,9 @@ const makeBody = value => {
return value
}

const resolvable = () => {
let _resolve
let _reject
let p = new Promise((resolve, reject) => (
[_resolve, _reject] = [resolve, reject])
)
p.resolve = _resolve
p.reject = _reject
return p
}

class R2 {
constructor (...args) {
this.opts = { method: 'GET' }
this.response = resolvable()
this._headers = {}
this._caseless = caseless(this._headers)

Expand All @@ -56,9 +44,7 @@ class R2 {
)
this._args(...args)

setTimeout(() => {
this._request()
}, 0)
this.response = Promise.resolve().then(() => this._request())
}

_args (...args) {
Expand Down Expand Up @@ -96,9 +82,7 @@ class R2 {

this.opts.headers = makeHeaders(this._headers)

fetch(url, this.opts)
.then(resp => this.response.resolve(resp))
.catch(err => this.response.reject(err))
return fetch(url, this.opts)
}
setHeaders (obj) {
for (let key in obj) {
Expand Down

0 comments on commit 012ce00

Please sign in to comment.