Open retorquere opened 9 years ago
@retorquere I looked into doing it, and everything I could find is really ugly. node's http libraries don't support synchronous requests, so I'd have to do a synchronous spawn and do all the processing in a child. This is a lot of work for a feature that most Web platform folks want deprecated.
On the bright side, there seem to be a few libraries doing synchronous HTTP, and that wasn't the case last time I looked into implementing synchronous XHR.
I've found some, thanks. For my case, I don't see a way around synchronous.
@retorquere Do you happen to have any recommendations? I'm working on a CLI app, and asynchronous behavior is breaking core functionality.
xmlhttprequest package supports synchronous yet
Ran into a painful debugging scenario before I realised that this module doesn't support sync XMLHttpRequest's. I might open a PR to at least throw an error if someone attempts to use this, to make debugging easier.
I created to-sync today
it utilize web workers instead of spawning sync processes and data is transfered synchronous over SharedArrayBuffer
so less code needs to be copied over.
Therefore it's also more compatible with other enviorments like Deno, Bun, and also Web Workers.
it's as simple as just doing:
import { createWorker } from 'to-sync'
const toSync = createWorker()
const fetchSync = toSync(async function (...args) {
const res = await fetch(...args)
const ab = await res.arrayBuffer()
return new Uint8Array(ab)
})
const uint8 = fetchSync(url)
const text = new TextDecoder().decode(uint8)
const json = JSON.parse(text)
const blob = new Blob([uint8])
check it out... it's very easy to use. all the arguments is easily transferable over to the worker cuz it can use structural clone alg instead of passing things around with argv flags. or over stdin/out
I'm trying to d a synchronous POST, but I'm getting "Synchronous XHR processing not implemented". code: