3 ms·
The most surprising thing that's bitten me about async/await is that the promise is immediately executed rather than being executed at the point they get awaite
by plopz 3y ago
The most surprising thing that's bitten me about async/await is that the promise is immediately executed rather than being executed at the point they get awaited.
- jezzamon 3y agoDefinitely something to know, but it's the best way for that to happen when you're waiting on asynchronous requests, as it lets you create the subsequent promises while the first promises is fetching its results
- plopz 3y agoIt hurts a lot though when you have a bunch of promises and you want to rate limit the execution
- vlovich123 3y agoWrap them in a generator.
- threatofrain 3y agoWhat's the status of cancellable promises in proposal land?
- capableweb 3y agoI mean, why would you want to "cancel" a Promise? You can already cancel HTTP requests, what are some other things you want to cancel really? If you don't want to continue with something inside a Promise, just pass in something that could be changed from `true` to `false` and it cancels itself based on that.
- coke12 3y agoI've had this same cancellation conversation many times with JS programmers. I think it's fair to say there's a need for standardization. Evidence for this need is network request management is really poor in pretty much all JS apps.
- capableweb 3y agoSpecifically the case of request management, it's already implemented and available in all browsers practically. It's like four lines of code you have to add/change, to be able to cancel a request. https://developer.mozilla.org/en-US/docs/Web/API/AbortController https://developer.mozilla.org/en-US/docs/Web/API/AbortContro... Regarding other cases, there are already ways of handling those. I don't think just because many ask about something, doesn't mean it's not currently working.
- coke12 3y agoThere are a lot of issues with the ergonomics of AbortController. It's very low level.
- capableweb 3y agoDid you actually look at the link I sent you? It's absolutely not low level. If you know how to use window.fetch, you know how to use AbortController already. controller = new AbortController(); const signal = controller.signal; fetch("http://example.com", { signal: signal }) // abort request controller.abort(); It basically couldn't be simpler? What sort of API would you like to be able to cancel requests?
- coke12 3y agoThat's a toy example! In reality in a complex app you'd usually be aborting far from where the fetch is created, and you'd have large promise chains where the semantics of cancellation are non-obvious and even unintuitive.
- capableweb 3y agoEh, it's so easy to provide your own implementation that does this, but I guess it wouldn't hurt to include in the standard-library since we already have something like Promise.all() Here is the implementation I typically use for doing just that: async function executeQueue(promises, concurrentLimit) { let index = 0; let activePromises = []; async function manageQueue() { if (index >= promises.length && activePromises.length === 0) return; while (activePromises.length < concurrentLimit && index < promises.length) { const promise = promises[index](); activePromises.push(promise); index++; promise.then(() => { activePromises = activePromises.filter(p => p !== promise); manageQueue(); }); } } await manageQueue(); await Promise.all(activePromises); } Used like this: function r(i, resolve) { console.log(`Task ${i}`) resolve(`Task ${resolve}`) } // Example usage const promises = [ () => new Promise(resolve => setTimeout(() => r("Task 1", resolve), 2000)), () => new Promise(resolve => setTimeout(() => r("Task 2", resolve), 1000)), () => new Promise(resolve => setTimeout(() => r("Task 3", resolve), 3000)), () => new Promise(resolve => setTimeout(() => r("Task 4", resolve), 500)), () => new Promise(resolve => setTimeout(() => r("Task 5", resolve), 1500)) ]; executeQueue(promises, 4).then(() => { console.log("All tasks completed!"); }); Would print: "Task Task 4" "Task Task 2" "Task Task 1" "Task Task 5" "Task Task 3" "All tasks completed!"