5 ms·
This is already a feature in Vanilla.js [1] in the "Parentheses" module. Example: const lines = (await fetch(myUrl)).text().split('\n'); [1] http://vanilla
by mnr 8y ago
This is already a feature in Vanilla.js [1] in the "Parentheses" module.
Example:
const lines = (await fetch(myUrl)).text().split('\n');
[1] http://vanilla-js.com/ http://vanilla-js.com/
Edit: magic-promises promotes all accessed properties to magic-promises as well, and performs magic on functions, so it's not exactly the same, but I'm not sure if I'd want to add the proxy overhead to my code just to make my code more "fluent".
- zebracanevra 8y agofetch's Response.text also returns a Promise, so you'd have to await twice: const lines = (await (await fetch(myUrl)).text()).split('\n');
- mnr 8y agoShucks, I can see how it becomes verbose.
- mikewhy 8y agoI've always just added an overload: function fetchText(input?: string | Request, init?: RequestInit) { return fetch(input, init).then(x => x.text()) } function fetchJson(input?: string | Request, init?: RequestInit) { return fetch(input, init).then(x => x.json()) }
- franciscop 8y agoAs explained .text() returns a promise, and the fact that many people don't know that and that your 1-line solution does not work is a strong indication that my solution is helpful! With magic-promises you don't need to worry about whether something regurns a promise or not. See fs-array, some methods will return promises and some won't, but you can just treat them all the same.
- mnr 8y agoI thought I was so clever! But yes I see how it's very different. In general, magic (or using Proxy) scares me, but I'm warming up to the idea here.