[Polyfill > Request] Fix not returning self after on(), rewrite to use optional chaining not ifs
This commit is contained in:
parent
930fc951dd
commit
0c7705f36a
1 changed files with 13 additions and 8 deletions
|
@ -54,30 +54,35 @@ const request = (options, callback) => { // Main function
|
||||||
const isError = !res.agent;
|
const isError = !res.agent;
|
||||||
|
|
||||||
if (isError) {
|
if (isError) {
|
||||||
if (listeners['error']) listeners['error'](res);
|
listeners['error']?.(res);
|
||||||
if (callback) callback(res, null, null); // Return null for others?
|
callback?.(res, null, null); // Return null for others?
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listeners['response']) listeners['response'](res);
|
listeners['response']?.(res);
|
||||||
if (!callback) return;
|
|
||||||
|
|
||||||
let body = '';
|
let body = '';
|
||||||
res.setEncoding('utf8');
|
res.setEncoding('utf8');
|
||||||
|
|
||||||
res.on('data', (chunk) => body += chunk);
|
res.on('data', (chunk) => {
|
||||||
|
body += chunk;
|
||||||
|
listeners['data']?.(chunk);
|
||||||
|
});
|
||||||
|
|
||||||
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
|
await new Promise((resolve) => res.on('end', resolve)); // Wait to read full body
|
||||||
|
|
||||||
callback(undefined, res, body);
|
callback?.(undefined, res, body);
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
const ret = {
|
||||||
on: (type, handler) => {
|
on: (type, handler) => {
|
||||||
listeners[type] = handler;
|
listeners[type] = handler;
|
||||||
|
return ret; // Return self
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Method functions
|
// Method functions
|
||||||
|
|
Loading…
Reference in a new issue