In the package Super Agent, the docs state:
A request can be initiated by invoking the appropriate method on the request object, then calling .then() (or .end() or await) to send the request.
request
.get('/search')
.then(res => {
// res.body, res.headers, res.status
});
However SuperAgent supports method chaining such as
request.get('/search')
.set('header1','value')
.set('header2','value')
to modify the request before it is sent. So...
My theory is that any chain off of the request
object returns a thenable object which is able to be await
'd or .then()
'd, and when it is, it will fire off a request and return an actual promise.
I looked around in the superagent repo and couldn't find anything like that. How else might waiting to send the request until the method chain is complete be accomplished?
You can never go wrong by just going to the source and looking. So, if you look at the code for .then()
, you will see that it calls .end()
on itself.
So, the request is sent when .then()
or .end()
is called. Up until then, other methods are just configuring the request object.
And, using await
will invoke .then()
on the promise.
Also, if you search for .end
in that above source file reference, you will see dozens of code examples that all show .end()
at the end of the chain. That is the original design architecture for superagent. Then, when promises came along, support for .then()
was added and it uses .end()
internally.