JS: futoin-request
FutoIn AsyncSteps friendly wrapper of request.
Usage
const $as = require('futoin-asyncsteps');
const $as_request = require('futoin-request');
$as().add((as) => {
// Very basic
$as_request(as, 'https://httpbin.org/get');
as.add((as, rsp, body) => console.log(body));
// As usual
$as_request.post(as, {
url: 'https://httpbin.org/post',
json: {a: 1, b: 2},
});
as.add((as, rsp, body) => console.log(body));
// With callback for request as stream manipulation
$as_request.post(as, {
url: 'https://httpbin.org/post',
headers: { 'content-length': 4 },
}, (req) => req.end('test') );
as.add((as, rsp, body) => console.log(body));
}).execute();
API notes
API is absolutely the same as for original request package except that:
- The first parameter must be a reference of AsyncSteps interface type.
- A next step receiving
(as, rsp, body) => {}
must be added instead of result callback. - Error is thrown through
AsyncSteps#error()
, if detected. - HTTP status != 200 is error as well.
- Request object is not returned, but passed to optional callback (third argument).
Additional notes:
- Request is properly canceled on
AsyncSteps#cancel()
or timeout -
Error info details
- error type -
RequestError
- error object should be available through standard
as.state.last_exception
convention as.state.last_response
is set with response object
- error type -
- Browser provides
$as_request
global reference